Иван обнови решението на 22.03.2015 19:23 (преди над 9 години)
+def extract_type(args, type_):
+ return ''.join([str(arg[0])*arg[1] for arg in args
+ if type(arg[0]) is type_])
+
+
+def reversed_dict(arg):
+ return dict([[value, key] for key, value in arg.items()])
+
+
+def flatten_dict(arg):
+ def expand(key_, value_):
+ if isinstance(value_, dict):
+ return [(key_ + '.' + key, value)
+ for key, value in flatten_dict(value_).items()]
+ return [(key_, value_)]
+
+ return dict([item for key, value in arg.items()
+ for item in expand(key, value)])
+
+
+def unflatten_dict(arg, result=None):
+ if result is None:
+ result = {}
+
+ for element in arg:
+ if '.' in element:
+ if element.split('.')[0] in result:
+ result[element.split('.')[0]] = unflatten_dict(
+ {element[len(element.split('.')[0]) + 1:]: arg[element]},
+ result[element.split('.')[0]])
+ else:
+ result[element.split('.')[0]] = unflatten_dict(
+ {element[len(element.split('.')[0]) + 1:]: arg[element]},
+ {})
+ else:
+ result[element] = arg[element]
+
+ return result
+
+
+def reps(arg):
+ return tuple([_ for _ in arg if arg.count(_) > 1])