简体   繁体   中英

Dictionary Comprehension for list values

I want to know if there's a more Pythonic way of doing the following, perhaps using dictionary comprehensions:

A = some list
D = {}
for i,v in enumerate(A):
    if v in D:
        D[v].append(i)
    else:
        D[v] = [i]

Using defaultdict :

from collections import defaultdict
D = defaultdict(list)
[D[v].append(i) for i, v in enumerate(A)]

Using setdefault :

D = {}
[D.setdefault(v, []).append(i) for i, v in enumerate(A)]

I can't figure any mean to use a dictionnary comprehension without sorting the data:

from itertools import groupby
from operator import itemgetter
{v: ids for v, ids in groupby(enumerate(sorted(A)), itemgetter(1))}

Performances:

from collections import defaultdict
from itertools import groupby
from operator import itemgetter
from random import randint

A = tuple(randint(0, 100) for _ in range(1000))

def one():
    D = defaultdict(list)
    [D[v].append(i) for i, v in enumerate(A)]

def two():
    D = {}
    [D.setdefault(v, []).append(i) for i, v in enumerate(A)]

def three():
    {v: ids for v, ids in groupby(enumerate(sorted(A)), itemgetter(1))}


from timeit import timeit

for func in (one, two, three):
    print(func.__name__ + ':', timeit(func, number=1000))

Results (as always, the simplest win):

one: 0.25547646999984863
two: 0.3754340969971963
three: 0.5032370890003222

You can do the following

d = collections.defaultdict(list)
for i,v in enumerate(A):
    d[v].append(i)

You can see that the values of the resulting dictionary are list s, the elements of which are to be produced while traversing. If you insist on doing a dict comp, you have to first find all the (value, [indices]) , then do a dict comp on [(k,[v])] , which just means extra acrobatics without any benefit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM