[英]Count the number of times an item occurs in each nested list
So I have a list of whether or not people won, drew or lost a game over each time they played: 因此,我列出了人们每次玩游戏是否赢,输还是输的列表:
scores = [["win","lose","win"],["win","win"],["draw","win"],["lose"]]
And I want to get a count of 'win'
/ 'lose'
/ 'draw'
for each sublist 我想为每个子列表计算'win'
/ 'lose'
/ 'draw'
Can I do this using a dictionary? 我可以使用字典吗?
eg dict= {[win:2, draw:0, lose:1],[win:2, draw:0, lose:0].....}
例如dict= {[win:2, draw:0, lose:1],[win:2, draw:0, lose:0].....}
I tried counting and placing in a list by doing this: 我尝试通过执行以下操作计数并放置在列表中:
countlose=0
for sublist in scores:
for item in sublist:
for item in range(len(sublist)):
if item=="lose":
countlose+=1
print(countlose)
But this just returned 0 但这只是返回0
Let me know how you would solve the problem 让我知道您将如何解决问题
Your desired result isn't valid syntax. 您想要的结果是无效的语法。 Most likely you want a list of dictionaries. 您最有可能需要字典列表。
collections.Counter
only counts values in an iterable; collections.Counter
仅对可迭代值进行计数; it does not count externally supplied keys unless you supply additional logic. 除非您提供其他逻辑,否则它不计算外部提供的密钥。
In this case, you can use a list comprehension, combining with an empty dictionary: 在这种情况下,您可以使用列表推导和空字典:
from collections import Counter
scores = [["win","lose","win"],["win","win"],["draw","win"],["lose"]]
empty = dict.fromkeys(('win', 'lose', 'draw'), 0)
res = [{**empty, **Counter(i)} for i in scores]
[{'draw': 0, 'lose': 1, 'win': 2},
{'draw': 0, 'lose': 0, 'win': 2},
{'draw': 1, 'lose': 0, 'win': 1},
{'draw': 0, 'lose': 1, 'win': 0}]
Can I do this using a dictionary? 我可以使用字典吗?
You can use collections.Counter
, which is a subclass of dict
for counting hashable objects: 您可以使用collections.Counter
,它是dict
的子类,用于计算可哈希对象:
>>> from collections import Counter
>>> scores = [['win', 'lose', 'win'], ['win', 'win'], ['draw', 'win'], ['lose']]
>>> counts = [Counter(score) for score in scores]
>>> counts
[Counter({'win': 2, 'lose': 1}), Counter({'win': 2}), Counter({'draw': 1, 'win': 1}), Counter({'lose': 1})]
To add zero counts for missing keys you can use an additional loop: 要为丢失的键添加零计数,可以使用其他循环:
>>> for c in counts:
... for k in ('win', 'lose', 'draw'):
... c[k] = c.get(k, 0)
...
>>> counts
[Counter({'win': 2, 'lose': 1, 'draw': 0}), Counter({'win': 2, 'lose': 0, 'draw': 0}), Counter({'draw': 1, 'win': 1, 'lose': 0}), Counter({'lose': 1, 'win': 0, 'draw': 0})]
Alternatively, you could wrap the counters with collections.defaultdict
: 或者,您可以使用collections.defaultdict
将计数器包装起来:
>>> counts = [defaultdict(int, Counter(score)) for score in scores]
>>> counts
[defaultdict(<class 'int'>, {'win': 2, 'lose': 1}), defaultdict(<class 'int'>, {'win': 2}), defaultdict(<class 'int'>, {'draw': 1, 'win': 1}), defaultdict(<class 'int'>, {'lose': 1})]
>>> counts[0]['draw']
0
You could apply a list comprehension for every sublist
from your given list. 您可以对给定列表中的每个sublist
应用列表理解 。
Also, declare your own counter
function which counts the number of appearances of one item from win
, lose
or draw
. 另外,声明您自己的counter
函数,该函数计算win
, lose
或draw
的一项出现的次数。
scores = [["win","lose","win"],["win","win"],["draw","win"],["lose"]]
def get_number(sublist):
counter = {'win': 0, 'draw' : 0, 'lose': 0}
for item in sublist:
counter[item] += 1
return counter
result = [get_number(sublist) for sublist in scores]
Output 产量
[{'win': 2, 'draw': 0, 'lose': 1},
{'win': 2, 'draw': 0, 'lose': 0},
{'win': 1, 'draw': 1, 'lose': 0},
{'win': 0, 'draw': 0, 'lose': 1}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.