mid*_*idi 5 python dictionary defaultdict
在Python中,我想要类似的东西
dict = defaultdict((list,list))
Run Code Online (Sandbox Code Playgroud)
本质上,对于每个键我都想要两个列表!
通过上面的代码片段,我得到错误第一个参数必须是可调用的。我怎样才能做到这一点?
作为参数提供给defaultdict创建空列表的函数:
from collections import defaultdict
def pair_of_lists():
return [[], []]
d = defaultdict(pair_of_lists)
d[1][0].append(3)
d[1][1].append(42)
print(d)
# defaultdict(<function pair_of_lists at 0x7f584a40b0d0>, {1: [[3], [42]]})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3073 次 |
| 最近记录: |