Мартина обнови решението на 23.03.2015 01:27 (преди над 9 години)
+def extract_type(element_list, characters):
+ word = ''
+ for element in element_list:
+ if type(element[0]) is characters:
+ for i in range(element[1]):
+ word = word + str(element[0])
+ return word
+
+
+def reversed_dict(dictionary):
+ return dict(map(lambda x: (dictionary[x], x), dictionary.keys()))
+
+
+def modify_keys(dictionary, k):
+ if len(dictionary) == 0:
+ return {}
+ else:
+ (key, value) = dictionary.popitem()
+ return dict({str(k + '.' + key): value}, **modify_keys(dictionary, k))
+
+
+def flatten_dict(dictionary):
+ if len(dictionary) == 0:
+ return {}
+ else:
+ (key, value) = dictionary.popitem()
+ if type(value) is not dict:
+ return dict(flatten_dict(dictionary), **{key: value})
+ else:
+ return dict(flatten_dict(dictionary),
+ **flatten_dict(modify_keys(value, key)))
+
+
+def merge_dicts(a, b):
+ c = a.copy()
+ for key in b:
+ if key in c:
+ c.update({key: merge_dicts(a[key], b[key])})
+ else:
+ c[key] = b[key]
+ return c
+
+
+def unflatten_dict(dictionary):
+ if len(dictionary) == 0:
+ return {}
+ else:
+ (key, value) = dictionary.popitem()
+ if key.count('.') == 0:
+ return dict(unflatten_dict(dictionary), **{key: value})
+ else:
+ temp = dict({key[:key.find('.')]:
+ unflatten_dict({key[key.find('.') + 1:]: value})})
+ return merge_dicts(unflatten_dict(dictionary), temp)
+
+
+def reps(element_list):
+ return tuple(filter(lambda x: element_list.count(x) > 1, element_list))
Какво се случва в unflatten_dict
, ако никой от елементите не трябва да бъде обработван? Ами във flatten
?