Станислав обнови решението на 18.03.2015 00:25 (преди над 9 години)
+from collections import defaultdict, Counter
+
+
+def extract_type(encoded, kind):
+ return ''.join(str(x) * y for x, y in encoded if isinstance(x, kind))
+
+
+def reversed_dict(dictionary):
+ return {value: key for key, value in dictionary.items()}
+
+
+def prefix_keys(dictionary, prefix):
+ return {prefix + "." + key: value for key, value in dictionary.items()}
+
+
+def flatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ if isinstance(value, dict):
+ result.update(prefix_keys(flatten_dict(value), key))
+ else:
+ result[key] = value
+ return result
+
+
+def add_nested(dictionary, key_components, value):
+ nested_dictionary = dictionary
+ for component in key_components[:-1]:
+ if component not in nested_dictionary:
+ nested_dictionary[component] = {}
+ nested_dictionary = nested_dictionary[component]
+ nested_dictionary[key_components[-1]] = value
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ add_nested(result, key.split('.'), value)
+ return result
+
+
+def reps(items):
+ counts = Counter(items)
+ return tuple(item for item in items if counts[item] > 1)