Python list of dictionaries search

后端 未结 21 2418
-上瘾入骨i
-上瘾入骨i 2020-11-22 09:41

Assume I have this:

[
{\"name\": \"Tom\", \"age\": 10},
{\"name\": \"Mark\", \"age\": 5},
{\"name\": \"Pam\", \"age\": 7}
]

and by searchin

21条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 10:06

    I tested various methods to go through a list of dictionaries and return the dictionaries where key x has a certain value.

    Results:

    • Speed: list comprehension > generator expression >> normal list iteration >>> filter.
    • All scale linear with the number of dicts in the list (10x list size -> 10x time).
    • The keys per dictionary does not affect speed significantly for large amounts (thousands) of keys. Please see this graph I calculated: https://imgur.com/a/quQzv (method names see below).

    All tests done with Python 3.6.4, W7x64.

    from random import randint
    from timeit import timeit
    
    
    list_dicts = []
    for _ in range(1000):     # number of dicts in the list
        dict_tmp = {}
        for i in range(10):   # number of keys for each dict
            dict_tmp[f"key{i}"] = randint(0,50)
        list_dicts.append( dict_tmp )
    
    
    
    def a():
        # normal iteration over all elements
        for dict_ in list_dicts:
            if dict_["key3"] == 20:
                pass
    
    def b():
        # use 'generator'
        for dict_ in (x for x in list_dicts if x["key3"] == 20):
            pass
    
    def c():
        # use 'list'
        for dict_ in [x for x in list_dicts if x["key3"] == 20]:
            pass
    
    def d():
        # use 'filter'
        for dict_ in filter(lambda x: x['key3'] == 20, list_dicts):
            pass
    

    Results:

    1.7303 # normal list iteration 
    1.3849 # generator expression 
    1.3158 # list comprehension 
    7.7848 # filter
    

提交回复
热议问题