我有一个列表,其中包含来自战斗的一定数量的获胜者,由用户输入,例如winners = ['Hunt', 'Nunes', 'Cormier']
然后我有一个字典,每个玩家的正确猜测值和他们的名字作为键,我的程序然后将获胜者列表与每个键的值进行比较,并告诉每个玩家他们猜对了多少次战斗
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.')
我希望能够做的是根据有多少人正确猜测每场比赛的值,然后使用此值来确定每个玩家正确猜到该战斗的胜利者将得到多少底池。
编辑:以下是我的全部代码,以帮助提供清晰度
#! 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.')
答案 0 :(得分:0)
以下计算正确猜测的数量&#39; players
字典中的一位玩家已经制作完成。如果是这样,您可以使用intersection()
list
set
winners
来执行此操作。
除此之外,还有一个获奖者名单。名称将添加到正在运行的total
中,然后将其转换为Counter集合(接受可迭代并返回键/值数据结构,该数据结构保持计数在列表中找到相同键的出现)。由此我们可以推断出必要的猜测数据。
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.'
或者你可以采用更强大的功能编程方法(受用户启发:@AMomchilov's想法)。
players_num_correct_picks = dict((k, winners.intersection(v)) for (k, v) in players.items())
for player, picks in players_num_correct_picks.items():
print player, 'guessed:', len(picks), 'fights correctly.'
total = Counter(item for sublist in players_num_correct_picks.values() for item in sublist)
for fighter, guess in total.items():
print fighter,'was guessed correctly',guess,'times.' if guess>1 else 'time.'
示例输出:
>>> 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.