Мария обнови решението на 19.03.2015 23:46 (преди над 9 години)
+def extract_type(primary, type_of_elements):
+ result = []
+ for x in primary:
+ if type(x[0]) is type_of_elements:
+ result.append(x[0] * x[1])
+ string_result = ''.join(result)
+ return string_result
+
+
+def reversed_dict (primary = dict()):
+ transposed = dict((second, first) for first, second in primary.items())
+ return transposed
+
+
+def flatten_dict(primary):
+ def expand(first, second):
+ if isinstance(second, dict):
+ return [ (first + '.' + key, value) for key, value in flatten_dict(second).items() ]
+ else:
+ return [ (first, second) ]
+ items = [ item for key, value in primary.items() for item in expand(key, value) ]
+ return dict(items)
+
+
+def unflatten_dict(primary):
+ new_dict = dict()
+ for first, second in primary.items():
+ parts = first.split(".")
+ dictionry = new_dict
+ for part in parts[:-1]:
+ if part not in dictionry:
+ dictionry[part] = dict()
+ dictionry = dictionry[part]
+ dictionry[parts[-1]] = second
+ return new_dict
+
+
+def reps(primary):
+ new_list = []
+ for x in primary:
+ if primary.count(x) > 1:
+ new_list.append(x)
+ return new_list