我用了两个小时的时间在谷歌上搜索,但没有找到答案。希望你能给我一些建议。
我正在寻找一个python结构,在那里我可以存储一个项目列表与他们的出现。例如:
{["item1","property1"]:10,
["item2","property2"]:5,
["item3","property3"]:5}
然后我可以调用像popitem()
这样的东西,它会返回例如["item3","property3"]结构会更新为以下内容:
{["item1","property1"]:10,
["item2","property2"]:5,
["item3","property3"]:4}
任何想法?谢谢!
您需要dict
的一个子类专门用于计数出现次数。这个子类已经存在:collection.Counter
.
注意Counter
,像所有dict
一样,要求它的键是可哈希的对象,因此:
- 你可以有
Counter({("item1","property1"):10, ("item2","property2"):5, ("item3","property3"):4})
; - 你不能有
Counter({["item1","property1"]:10, ["item2","property2"]:5, ["item3","property3"]:4})
.
Counter with random.choices的演示
import collection
import random
l = [('item1', 'property1'), ('item1', 'property1'), ('item1', 'property1'), ('item1', 'property1'), ('item1', 'property1'), ('item1', 'property1'), ('item1', 'property1'), ('item1', 'property1'), ('item1', 'property1'), ('item1', 'property1'), ('item2', 'property2'), ('item2', 'property2'), ('item2', 'property2'), ('item2', 'property2'), ('item2', 'property2'), ('item3', 'property3'), ('item3', 'property3'), ('item3', 'property3'), ('item3', 'property3')]
c = collections.Counter(l)
print(c)
# Counter({('item1', 'property1'): 10, ('item2', 'property2'): 5, ('item3', 'property3'): 4})
for i in range(10):
item, prop = random.choices(list(c.keys()), weights = list(c.values()), k = 1)[0]
print(item, prop, end=', ')
c[(item, prop)] -= 1
# item1 property1, item1 property1, item2 property2,
# item2 property2, item3 property3, item1 property1,
# item1 property1, item3 property3, item1 property1,
# item1 property1,
print(c)
# Counter({('item1', 'property1'): 4, ('item2', 'property2'): 3, ('item3', 'property3'): 2})
计数器的演示
import collections
import random
c = collections.Counter({("item1","property1"):10, ("item2","property2"):5, ("item3","property3"):4})
popped = random.sample(list(c.keys()), 10, counts=list(c.values()))
c = c - collections.Counter(popped)
print(popped)
# [('item2', 'property2'), ('item2', 'property2'), ('item3', 'property3'),
# ('item1', 'property1'), ('item1', 'property1'), ('item1', 'property1'),
# ('item3', 'property3'), ('item1', 'property1'), ('item1', 'property1'),
# ('item3', 'property3')]
print(c)
# Counter({('item1', 'property1'): 5, ('item2', 'property2'): 3, ('item3', 'property3'): 1})
在python中不能使用可变键(如列表)创建字典。你可以用元组来代替。
要使popitem()
像这样工作,您必须将字典包装在对象中并实现您自己的函数。
class youClass(dict):
def popitem(self):
key=next(iter(self.keys())) #get one key, will act similary to pop()
if self[key]>1:
self[key]-=1
else:
del self[key]
return key
你可以这样创建你的字典:
yourDict = yourClass({("item1","property1"):10,
("item2","property2"):5,
("item3","property3"):5})