我有一个包含几个元素的列表(0,1,2,3,...)
现在我有一个以上述唯一元素为键的字典,它们被视为值:
{0:1, 1:3, 2:4, 3:6}
Run Code Online (Sandbox Code Playgroud)
我想把它重建回一个列表
[0, 1,1,1, 2,2,2,2, 3,3,3,3,3,3]
Run Code Online (Sandbox Code Playgroud)
有没有什么pythonic的方式来实现这一点?香草或麻木。
from collections import Counter
c = Counter({0:1, 1:3, 2:4, 3:6})
list(c.elements())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
139 次 |
| 最近记录: |