Елена обнови решението на 23.03.2015 01:45 (преди над 9 години)
+import copy
+
+
+def extract_type(input, type):
+ result = ''
+ for element in input:
+ if isinstance(element[0], type):
+ result += (str(element[0]) * element[1])
+ return result
+
+
+def reversed_dict(input):
+ return dict(zip(input.values(), input.keys()))
+
+
+def flatten_dict(input):
+ copied = copy.deepcopy(input)
+ for key, value in copied.items():
+ if isinstance(value, dict):
+ for inner_item in value:
+ input[str(key) + '.' + inner_item] = value[inner_item]
+ del input[key]
+ for value in list(input.values()):
+ if isinstance(value, dict):
+ input.update(flatten_dict(input))
+ return input
+
+
+def unflatten_dict(input):
+ copied = copy.deepcopy(input)
+ for key, value in copied.items():
+ if '.' in key:
+ partitions = key.split('.', 1)
+ if partitions[0] not in input:
+ input[partitions[0]] = {partitions[1]: value}
+ elif isinstance(input[partitions[0]], dict):
+ input[partitions[0]].update({partitions[1]: value})
+ del input[key]
+ for value in list(input.values()):
+ if isinstance(value, dict):
+ value.update(unflatten_dict(value))
+ return input
+
+
+def reps(input):
+ return tuple(item for item in input if input.count(item) > 1)