How do I check the frequency of an item in a list and then if that item has a frequency of 4 remove all the matching items?
context:
trying to make a go fish game in python and I need to be able to check if a players hand has four matching numbers if the player's hand does then I need to remove all four of the matching items and increase there score by 1
input
score = 0
[1,2,4,3,5,6,1,1,1]
output
[2,4,3,5,6]
score += 1
the player's hand is a list of numbers.
here is the file for the game: ''' https://github.com/StarSpace-Interactive/GoFish/tree/master/GoFish
Here's a solution:
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)
And the output is:
[2, 4, 3, 5, 6]
1