Convert dictionary into list with length based on values

泪湿孤枕 提交于 2020-12-23 04:54:55

问题


I have a dictionary

d = {1: 3, 5: 6, 10: 2}

I want to convert it to a list that holds the keys of the dictionary. Each key should be repeated as many times as its associated value.

I've written this code that does the job:

d = {1: 3, 5: 6, 10: 2}
l = []
for i in d:
    for j in range(d[i]):
        l.append(i)
l.sort()
print(l)

Output:

[1, 1, 1, 5, 5, 5, 5, 5, 5, 10, 10]

But I would like it to be a list comprehension. How can this be done?


回答1:


You can do it using a list comprehension:

[i for i in d for j in range(d[i])]

yields:

[1, 1, 1, 10, 10, 5, 5, 5, 5, 5, 5]

You can sort it again to get the list you were looking for.




回答2:


[k for k,v in d.items() for _ in range(v)] ... I guess...

if you want it sorted you can do

[k for k,v in sorted(d.items()) for _ in range(v)]




回答3:


One approach is to use itertools.chain to glue sublists together

>>> list(itertools.chain(*[[k]*v for k, v in d.items()]))
[1, 1, 1, 10, 10, 5, 5, 5, 5, 5, 5]

Or if you are dealing with a very large dictionary, then you could avoid constructing the sub lists with itertools.chain.from_iterable and itertools.repeat

>>> list(itertools.chain.from_iterable(itertools.repeat(k, v) for k, v in d.items()))
[1, 1, 1, 10, 10, 5, 5, 5, 5, 5, 5]

Comparative timings for a very large dictionary with using a list comprehension that uses two loops:

>>> d = {i: i for i in range(100)}
>>> %timeit list(itertools.chain.from_iterable(itertools.repeat(k, v) for k, v in d.items()))
10000 loops, best of 3: 55.6 µs per loop
>>> %timeit [k for k, v in d.items() for _ in range(v)]
10000 loops, best of 3: 119 µs per loop

It's not clear whether you want your output sorted (your example code does not sort it), but if so simply presort d.items()

# same as previous examples, but we sort d.items()
list(itertools.chain(*[[k]*v for k, v in sorted(d.items())]))



回答4:


Counter.elements() method does exactly this:

from collections import Counter

d = {1: 3, 5: 6, 10: 2}
c = Counter(d)
result = list(c.elements())
print(result)
# [1, 1, 1, 5, 5, 5, 5, 5, 5, 10, 10]


来源:https://stackoverflow.com/questions/39987708/convert-dictionary-into-list-with-length-based-on-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!