Никола обнови решението на 23.03.2015 00:36 (преди над 9 години)
+from itertools import repeat
+
+
+def extract_type(items, i_type):
+ output = i_type()
+ for item in items:
+ if isinstance(item[0], i_type):
+ for i in repeat(item[0], item[1]):
+ output += i
+ return output
+
+
+def reversed_dict(dic):
+ return dict((value, key) for (key, value) in dic.items())
+
+
+def flatten_dict(dic):
+ if len(dic) == 0:
+ return {}
+
+ output = {}
+
+ for (key, value) in dic.items():
+ if not isinstance(value, dict):
+ output[key] = value
+ else:
+ output.update(dict((key + '.' + inner_key, inner_value)
+ for (inner_key, inner_value) in flatten_dict(value).items()))
+ return output
+
+
+def unflatten_dict(dic):
+ output = {}
+ for (key, value) in dic.items():
+ if key.find('.') == -1:
+ output[key] = value
+ else:
+ output = dict_merge(output, dictify(key, value, '.'))
+ return output
+
+
+def dictify(source, value, delimiter):
+ output, parts = {}, source.split(delimiter)
+ if len(parts) == 1:
+ return {source: value}
+
+ ref = output
+ for i in parts[:-1]:
+ ref[i] = dict()
+ ref = ref[i]
+ ref[parts[-1]] = value
+ return output
+
+
+def dict_merge(source, extension):
+ output = dict(source)
+ for (key, value) in extension.items():
+ if not isinstance(value, dict):
+ output[key] = value
+ elif key in output and isinstance(output[key], dict):
+ output[key] = dict_merge(output[key], value)
+ else:
+ output[key] = value
+ return output
+
+
+def reps(items):
+ return tuple(filter(lambda x: items.count(x) > 1, items))
Тествай extraxt_type
по-добре. Виж как работи и с типове различни от str