Pythonic way to access arbitrary element from dictionary

后端 未结 4 778
萌比男神i
萌比男神i 2020-12-06 16:33

I have a dictionary, full of items. I want to peek at a single, arbitrary item:

print("Amongst our dictionary\'s items are such diverse elements as: %s&q         


        
4条回答
  •  一个人的身影
    2020-12-06 16:52

    I believe the question has been significantly answered but hopefully this comparison will shed some light on the clean code vs time trade off:

    from timeit import timeit
    from random import choice
    A = {x:[y for y in range(100)] for x in range(1000)}
    def test_pop():
        k, v= A.popitem()
        A[k] = v
    
    def test_iter(): k = next(A.iterkeys())
    
    def test_list(): k = choice(A.keys())
    
    def test_insert(): A[0] = 0
    
    if __name__ == '__main__':
        print('pop', timeit("test_pop()", setup="from __main__ import test_pop", number=10000))
        print('iter', timeit("test_iter()", setup="from __main__ import test_iter", number=10000))
        print('list', timeit("test_list()", setup="from __main__ import test_list", number=10000))
        print('insert', timeit("test_insert()", setup="from __main__ import test_insert", number=10000))
    

    Here are the results:

    ('pop', 0.0021750926971435547)
    ('iter', 0.002003908157348633)
    ('list', 0.047267913818359375)
    ('insert', 0.0010859966278076172)
    

    It seems that using iterkeys is only marginal faster then poping an item and re-inserting but 10x's faster then creating the list and choosing a random object from it.

提交回复
热议问题