Кристиан обнови решението на 18.03.2015 20:17 (преди над 9 години)
+from functools import reduce
+
+
+def extract_type(list, valid_type):
+ result = [str(value) * count for (value, count)
+ in list if type(value) is valid_type]
+ return "".join(result)
+
+
+def update_and_return(dictionary, pair):
+ dictionary.update({pair[1]: pair[0]})
+ return dictionary
+
+
+def reversed_dict(dictionary):
+ return reduce(update_and_return, dictionary.items(), {})
+
+
+def flatten_dict_internal(result, context, dictionary):
+ for pair in dictionary.items():
+ new_context = context[:]
+ new_context.append(pair[0])
+
+ if type(pair[1]) is dict:
+ flatten_dict_internal(result, new_context, pair[1])
+ else:
+ result[".".join(new_context)] = pair[1]
+ return result
+
+
+def flatten_dict(dictionary):
+ return flatten_dict_internal({}, [], dictionary)
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for pair in dictionary.items():
+ current = result
+ keys = pair[0].split('.')
+ for key in keys[:-1]:
+ current[key] = current.get(key, {})
+ current = current[key]
+ current[keys[-1]] = pair[1]
+ return result
+
+
+def reps(items):
+ return tuple([item for item in items if items.count(item) > 1])
Хубаво е да смениш името list
, тъй като е запазена дума в езика :)