Людмил обнови решението на 22.03.2015 16:30 (преди над 9 години)
+import itertools
+
+
+def extract_type(symbols_list, type):
+ symbols = ""
+ for symbol_tuple in symbols_list:
+ if isinstance(symbol_tuple[0], type):
+ for _ in itertools.repeat(None, symbol_tuple[1]):
+ symbols += str(symbol_tuple[0])
+ return symbols
+
+
+def reversed_dict(dict):
+ reversed_dictionary = {}
+ for key in dict.keys():
+ if dict[key] not in reversed_dictionary:
+ reversed_dictionary[dict[key]] = key
+ return reversed_dictionary
+
+
+def flatten_dict(dictionary, path=""):
+ items = []
+ for key in dictionary.keys():
+ if path == "":
+ current_key = key
+ else:
+ current_key = "{0}.{1}".format(path, key)
+ if type(dictionary[key]) is dict:
+ items.extend(flatten_dict(dictionary[key], current_key).items())
+ else:
+ items.append((current_key, dictionary[key]))
+ return dict(items)
+
+
+def create_nested_dict_element(unnested_dict_element):
+ if len(unnested_dict_element[0]) == 1:
+ return dict([unnested_dict_element])
+ else:
+ current_key, rest = unnested_dict_element[0].split(".", maxsplit=1)
+ value = unnested_dict_element[1]
+ return {current_key: create_nested_dict_element((rest, value))}
+
+
+def add_dicts_nested(first, second):
+ for key in second.keys():
+ if key not in first.keys():
+ first[key] = second[key]
+ else:
+ add_dicts_nested(first[key], second[key])
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ add_dicts_nested(result, create_nested_dict_element((key, value)))
+ return result
+
+
+def reps(elements):
+ return tuple([item for item in elements if elements.count(item) > 1])