我花了两个小时在谷歌上搜索,找不到答案。希望你能给我一些建议
我正在寻找一个python结构,在那里我可以存储一个项目列表及其出现次数。例如:
{["item1","property1"]:10,
["item2","property2"]:5,
["item3","property3"]:5}
然后我可以调用类似popitem()
的东西,它将返回例如[“item3”,“property3”],结构将更新为以下内容:
{["item1","property1"]:10,
["item2","property2"]:5,
["item3","property3"]:4}
有什么想法吗?谢谢
您需要一个子类}
dict
专门用于计数事件。该子类已存在:^{请注意
Counter
与所有dict
一样,要求其键是可散列对象,因此:Counter({("item1","property1"):10, ("item2","property2"):5, ("item3","property3"):4})
李>Counter({["item1","property1"]:10, ["item2","property2"]:5, ["item3","property3"]:4})
李>随机选择计数器演示
随机样本计数器演示
在python中,不能使用可变键(如列表)生成dict。您可以改用元组
要使
popitem()
的行为类似于此,必须将字典包装在对象中并实现自己的函数您可以通过以下方式创建dict:
相关问题 更多 >
编程相关推荐