Иван обнови решението на 21.03.2015 02:11 (преди над 9 години)
+def extract_type(sequence, some_type):
+ return ''.join([str(item[0]) * item[1] for item in sequence
+ if type(item[0]) == some_type])
+
+
+def reversed_dict(dictionary):
+ return {value: key for (key, value) in dictionary.items()}
+
+
+def reps(sequence):
+ return tuple([item for item in sequence if sequence.count(item) != 1])
+
+
+def flatten(dictionary, result):
+ for key, value in dictionary.items():
+ if not isinstance(value, dict):
+ result[key] = value
+ else:
+ new_dict = {key + '.' + k: v for (k, v) in value.items()}
+ flatten(new_dict, result)
+ return result
+
+
+def flatten_dict(dictionary):
+ return flatten(dictionary, {})
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ key_items = key.rsplit('.')
+ d = result
+ for key_item in key_items[:-1]:
+ if key_item not in d:
+ d[key_item] = {}
+ d = d[key_item]
+ d[key_items[-1]] = value
+ return result