如何计算列表中项目的频率?
How do to Count the frequency of an item in a list?
如何检查列表中某个项目的频率,然后如果该项目的频率为 4,则删除所有匹配的项目?
上下文:
尝试在 python 中制作围棋游戏,我需要能够检查玩家的手是否有四个匹配的号码,如果玩家的手有,那么我需要删除所有四个匹配的项目并将分数提高 1
输入
score = 0
[1,2,4,3,5,6,1,1,1]
输出
[2,4,3,5,6]
score += 1
玩家的手是一列数字。
这是游戏的文件:
'''
https://github.com/StarSpace-Interactive/GoFish/tree/master/GoFish
这是一个解决方案:
from collections import Counter
score = 0
hand = [1,2,4,3,5,6,1,1,1]
counts = Counter(hand)
for num, count in counts.items():
if count >= 4:
hand = list(filter((num).__ne__, hand))
score += 1
print(hand)
print(score)
输出为:
[2, 4, 3, 5, 6]
1
from collections import defaultdict
score = 0
hand = [1,2,4,3,5,6,1,1,1] # current hand
# im guessing you are doing this in a loop
d= defaultdict( int )
for card in hand:
d[card] += 1
fourList = []
for key, value in d.items():
if value >= 4:
fourList.append(key)
hand = [n for n in hand if n not in fourList]
score += len(fourList)
print(hand)
print(score)
您可以通过 Counter
实现您的目标。例如,
from collections import Counter
mylist = [1,2,4,3,5,6,1,1,1]
counter = Counter(mylist)
那么,counter
就是
Counter({
1: 4,
2: 1,
4: 1,
3: 1,
5: 1,
6: 1
})
然后,你可以写一个python函数来更新分数和计数器。
def update_score(counter, curr_score):
remove_keys = list()
# update score
for key, value in counter.items():
if value >= 4:
curr_score += 1
remove_keys.append(key)
# remove key
for key in remove_keys:
del counter[key]
return counter, curr_score
它将 return 新的当前分数和更新的计数器返回。
就我个人而言,我发现 pandas
value_count
功能比上面建议的 numpy.histogram
更人性化。你可以像这样使用它,假设你的手是一个列表(当然,如果手是一个系列,这个解决方案更简单):
import pandas as pd
hand = [1,1,2,3,4,1,1]
cards_count = pd.Series.value_counts(hand)
# count how many times each card appears
score += (cards_count>=4).sum()
# add 1 to score for each card that repeats at least 4 times
hand = [card for card in hand if card not in cards_count.index[cards_count>=4]]
# keeps only cards that did not appear >=4 times
如何检查列表中某个项目的频率,然后如果该项目的频率为 4,则删除所有匹配的项目?
上下文:
尝试在 python 中制作围棋游戏,我需要能够检查玩家的手是否有四个匹配的号码,如果玩家的手有,那么我需要删除所有四个匹配的项目并将分数提高 1
输入
score = 0
[1,2,4,3,5,6,1,1,1]
输出
[2,4,3,5,6]
score += 1
玩家的手是一列数字。
这是游戏的文件: ''' https://github.com/StarSpace-Interactive/GoFish/tree/master/GoFish
这是一个解决方案:
from collections import Counter
score = 0
hand = [1,2,4,3,5,6,1,1,1]
counts = Counter(hand)
for num, count in counts.items():
if count >= 4:
hand = list(filter((num).__ne__, hand))
score += 1
print(hand)
print(score)
输出为:
[2, 4, 3, 5, 6]
1
from collections import defaultdict
score = 0
hand = [1,2,4,3,5,6,1,1,1] # current hand
# im guessing you are doing this in a loop
d= defaultdict( int )
for card in hand:
d[card] += 1
fourList = []
for key, value in d.items():
if value >= 4:
fourList.append(key)
hand = [n for n in hand if n not in fourList]
score += len(fourList)
print(hand)
print(score)
您可以通过 Counter
实现您的目标。例如,
from collections import Counter
mylist = [1,2,4,3,5,6,1,1,1]
counter = Counter(mylist)
那么,counter
就是
Counter({
1: 4,
2: 1,
4: 1,
3: 1,
5: 1,
6: 1
})
然后,你可以写一个python函数来更新分数和计数器。
def update_score(counter, curr_score):
remove_keys = list()
# update score
for key, value in counter.items():
if value >= 4:
curr_score += 1
remove_keys.append(key)
# remove key
for key in remove_keys:
del counter[key]
return counter, curr_score
它将 return 新的当前分数和更新的计数器返回。
就我个人而言,我发现 pandas
value_count
功能比上面建议的 numpy.histogram
更人性化。你可以像这样使用它,假设你的手是一个列表(当然,如果手是一个系列,这个解决方案更简单):
import pandas as pd
hand = [1,1,2,3,4,1,1]
cards_count = pd.Series.value_counts(hand)
# count how many times each card appears
score += (cards_count>=4).sum()
# add 1 to score for each card that repeats at least 4 times
hand = [card for card in hand if card not in cards_count.index[cards_count>=4]]
# keeps only cards that did not appear >=4 times