python字典到重复列表

本翰 *_*翰 張 4 python dictionary list

如何在python中将字典转换为重复列表?

例如:{'a':1,'b':2,'c':1,'d':3}to['a','b','b','c','d','d','d']

geo*_*org 5

Counter.elements 来自集合模块正是这样做的:

d = {'a':1,'b':2,'c':1,'d':3}
from collections import Counter
print sorted(Counter(d).elements())
# ['a', 'b', 'b', 'c', 'd', 'd', 'd']
Run Code Online (Sandbox Code Playgroud)