Мартин обнови решението на 23.03.2015 01:07 (преди над 9 години)
+def extract_type(symbols_reps_pair, wanted_type):
+ result = []
+ for (symbols, reps) in symbols_reps_pair:
+ if type(symbols) is wanted_type:
+ for _ in range(reps):
+ result.append(str(symbols))
+ return "".join(result)
+
+
+def reversed_dict(dictionary):
+ return {value: key for key, value in dictionary.items()}
+
+
+def reps(collection):
+ return tuple(element for element in collection
+ if collection.count(element) > 1)
+
+
+def flatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ if type(value) is dict:
+ for sub_key, sub_value in flatten_dict(value).items():
+ result[key + "." + sub_key] = sub_value
+ else:
+ result[key] = value
+ return result
+
+
+def get_all_elements_with_root(dictionary, root):
+ result = {}
+ for key, value in dictionary.items():
+ splitted_root_from_key = key.split(".", 1)
+ if splitted_root_from_key[0] == root:
+ result[splitted_root_from_key[1]] = value
+ return result
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ if "." in key:
+ splitted_root_from_key = key.split(".", 1)
+ root = splitted_root_from_key[0]
+ if root not in result:
+ result[root] = get_all_elements_with_root(dictionary, root)
+ result[root] = unflatten_dict(result[root])
+ else:
+ result[key] = value
+ return result