wallace11
5/20/2018 - 2:02 AM

Python

python3-setuptools
build-essential libssl-dev libffi-dev python3-dev
l = [
    {"test": "hello"},
    {"test": "world"},
    {"test": "world"},
    {"test": "hello"},
    {"test": "world"}
]

# Make a list of all values
values = list(map(lambda x: x['test'], l))

# Create a dictionary of {"value": Frequency}
freq = dict((x, values.count(x)) for x in set(values))

# Sort l by freq by matching each of l item's "test" value with freq's key
sorted(l, reverse=True, key=lambda x: freq[x['test']])

"""
Output:
[
  {'test': 'world'},
  {'test': 'world'},
  {'test': 'world'},
  {'test': 'hello'},
  {'test': 'hello'}
]
"""
def timing(f):
    def wrap(*args):
        time1 = time.time()
        ret = f(*args)
        time2 = time.time()
        print '%s function took %0.3f ms' % (f.func_name, (time2-time1)*1000.0)
        return ret
    return wrap
def dict_update(base, extra):
    if any(map(lambda x: isinstance(x, dict), base.values())):  # Check if there are any nested dicts
        for k, v in extra.items():
            try:
                base[k].update(v)
            except (KeyError, AttributeError):
                base[k] = v
    else:
        base.update(extra)
    return base