Luke Bray Luke Bray - 3 months ago 4
Python Question

How to compare list and dictionary values and assign a variable

I have a list with a certain amount of winners from fights, entered by the user e.g.

winners = ['Hunt', 'Nunes', 'Cormier']


I then have a dictionary with each player's correct guess as values and their names as keys and my program then compares the list of winners with the values for each key and tells each player how many fights they guessed correctly

for name in player_dict:
player_dict[name].sort()
player_dict[name] = set(player_dict[name]) & set(winners)
wins = (len(player_dict[name]))
print(name + ' guessed ' + str(wins) + ' fights correctly.')


What I want to be able to do is give each fight a value depending on how many people guessed correctly and then use this value to determine how much of the pot each player who correctly guessed the winner of that fight will get.

EDIT: Here is the entirety of my code to help provide clarity

#! python3

#fight_gambler.py - a program that lets players gamble on fights with friends

players = []
while len(players) >= 0:
name = input('Enter a name: ')
players.append(name)
if name == '':
players.pop()
break
else:
pass

player_dict = {name: [] for name in players} #creates a key for each name with that name as the key
print(player_dict)

fight_amount = int(input('How many fights are there? '))
fight_number = 1
for name in player_dict: #adds fight winner prediction as values to each name

fight_number = 1

while fight_number <= fight_amount:
answer = input(name + ', who will win fight ' + str(fight_number) + '? ')
player_dict[name].append(answer)
fight_number = fight_number + 1
fight_number = 1
winners = []

while fight_number <= fight_amount: # creates a list of fight winners
winner = input('Who won fight ' + str(fight_number) + '? ')
winners.append(winner)
fight_number = fight_number + 1
winners.sort()

for name in player_dict:
player_dict[name].sort()
player_dict[name] = set(player_dict[name]) & set(winners)
wins = (len(player_dict[name]))
print(name + ' guessed ' + str(wins) + ' fights correctly.')

Answer

The following calculates the number of correct guess' a player in the players dictionary has made. If so you can do this with the intersection() of a list with a set of winners.

To add to this, a list of the winners' names is added to a running total, this is then converted to a Counter collection (accepts an iterable and returns a key/value data structure that keeps count of occurrences of the same strings found in the list). From this we can extrapolate the necessary guessing data.

from collections import Counter

winners = set([['Hunt', 'Nunes', 'Cormier'])
players = {'player1': ['Nunes', 'Cormier', 'test1', 'test2'], 
'player2': ['Nunes','test1', 'test2','test3', 'test4'],  
'player3' : ['Hunt', 'Nunes', 'Cormier']}

total = []
for player, picks in players.iteritems():
    inter = winners.intersection(picks)
    total.extend(inter)  # Add to list of winner names.
    print player, 'guessed:', len(inter), 'fights correctly.'

for fighter, guess in Counter(total).iteritems():
    print fighter,'was guessed correctly',guess,'times.' if guess>1 else 'time.'

Sample Output:

>>> player2 guessed: 1 fights correctly.
>>> player3 guessed: 3 fights correctly.
>>> player1 guessed: 2 fights correctly.
>>> Cormier was guessed correctly 2 times.
>>> Nunes was guessed correctly 3 times.
>>> Hunt was guessed correctly 1 time.
Comments