Любомир обнови решението на 23.03.2015 00:52 (преди над 9 години)
+def extract_type(txt, data_type):
+ return ''.join([el[0] * el[1] for el in txt if type(el[0]) == data_type])
+
+
+def reversed_dict(values):
+ return {value: key for key, value in values.items()}
+
+
+def flatten_dict(values, p_key=False):
+ sep = '.'
+ result = []
+ for key, value in values.items():
+ new_key = p_key + sep + key if p_key else key
+ if type(value) == dict:
+ result.extend(flatten_dict(value, new_key).items())
+ else:
+ result.append((new_key, value))
+
+ return dict(result)
+
+
+def unflatten_dict(values):
+ sep = '.'
+ unflatten = {}
+ for path, value in values.items():
+ keys = path.split('.')
+ res = unflatten
+ while len(keys) > 1:
+ key = keys.pop(0)
+ if key not in res:
+ res[key] = {}
+ res = res[key]
+
+ key = keys.pop()
+ res[key] = value
+
+ return unflatten
+
+
+def reps(values):
+ return tuple([el for el in values if values.count(el) > 1])