Mapping a set of key values to zero
我正在通过for循环输入一组键。
1 2 3 4 5 6 7 | myDict = {} for i in range(n): team = input().split() t1 = team[0] t2 = team[1] |
输入的值(n=10)为:
1 2 3 4 5 6 7 8 9 10 | Truckers Trekkers Riders Bikers Wanderers Rovers Rovers Riders Trekkers Wanderers Bikers Truckers Wanderers Bikers Truckers Rovers Riders Trekkers Trekkers Wanderers |
在for循环中,我一直在尝试将这些名称初始化为字典中的值0。例如,这是我想要的最终结果:
1 | myDict = {'Truckers': 0, 'Trekkers':0,'Riders':0,'Bikers':0,'Wanderers':0,'Rovers':0} |
为了实现这一点,我尝试使用
1 | myDict = dict(zip(lst, lst1)) |
但是,尽管这会得到结果,但只有在for循环完成之后才会得到结果。在for循环中,是否有任何方法可以将上述输入值添加到字典中?例如,当您输入'trucker''trekkers'时,它将被添加到mydict中,值为0(例如
编辑:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Truckers Trekkers 87 75 Riders Bikers 80 90 Wanderers Rovers 53 81 Rovers Riders 47 51 Trekkers Wanderers 72 70 Bikers Truckers 25 30 Wanderers Bikers 40 35 Truckers Rovers 50 55 Riders Trekkers 61 45 Trekkers Wanderers 70 73 |
对于本联盟的每支球队,第一支球队(T1)代表主队,第二支球队代表客队(T2),第三和第四个论点分别代表每支球队的得分(即卡车司机得分87分,Trekkers得分75分)。在这个联盟中,当主队获胜时,他们会得到3000分,当客队赢了-3500分,平局-1000分,输了-50分。
在第一种情况下,卡车司机是主队,他们赢了(87>75),所以他们得到3000分加上比赛中的87分。此外,由于特雷克斯队输了,该队从比赛结果中获得50分(由于输了)和75分。因此,卡车司机将增加3087辆,徒步旅行者将增加125辆。在第二种情况下,车手是主队,车手是客队,因为80<90。骑车者得3500分+90分(3590分);骑车者得3000分+80分(3080分)。所以现在,mydict看起来是这样的:
1 | {'Truckers': 3087, 'Trekkers': 125, 'Bikers': 3590, 'Riders': 3080} |
最后,字典应该查找以下内容:
1 | {'Wanderers': 6836, 'Truckers': 6717, 'Bikers': 3750, 'Rovers': 7233, 'Riders': 6742, 'Trekkers': 3412} |
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | if __name__ == '__main__': n = int(input()) homeWin = 3000 awayWin = 3500 draw = 1000 loss = 50 myDict = {} lst = [] lst1 = [] myDict[t1] = 0 myDict[t2] = 0 for n_itr in range(n): team = input().split() t1 = team[0] t2 = team[1] p1 = int(team[2]) p2 = int(team[3]) #lst.append(t1) #lst1.append(0) #myDict.update(dict.fromkeys(lst, 0)) #myDict = dict(zip(lst, lst1)) if p1 > p2: myDict[t1] += p1 + homeWin myDict[t2] += p2 + loss elif p1 < p2: myDict[t2] += p2 + awayWin myDict[t1] += p1 + loss elif p1 == p2: myDict[t1] += t1 + draw myDict[t2] += t2 + draw print(myDict) #print(max(myDict.items(), key=operator.itemgetter(1))[0]) #print(min(myDict.items(), key=operator.itemgetter(1))[0]) |
它消除了将每个队的起始分数设为0的需要。它还消除了在运行算法之前定义键的需要。
解决方案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | from collections import Counter lst = ['Truckers Trekkers 87 75', 'Riders Bikers 80 90', 'Wanderers Rovers 53 81', 'Rovers Riders 47 51', 'Trekkers Wanderers 72 70', 'Bikers Truckers 25 30', 'Wanderers Bikers 40 35', 'Truckers Rovers 50 55', 'Riders Trekkers 61 45', 'Trekkers Wanderers 70 73'] c = Counter() for i in lst: home, away, home_score, away_score = i.split() home_score = int(home_score) away_score = int(away_score) if home_score > away_score: c[home] += 3000 + home_score c[away] += 50 + away_score elif away_score > home_score: c[home] += 50 + home_score c[away] += 3500 + away_score elif home_score == away_score: c[home] += 1000 + home_score c[away] += 1000 + away_score |
结果
1 2 3 4 5 6 | Counter({'Bikers': 3750, 'Riders': 6742, 'Rovers': 7233, 'Trekkers': 3412, 'Truckers': 6717, 'Wanderers': 6836}) |
解释
collections.Counter 就像一个简单的计数器,用于提供任何密钥,即使它们没有被显式添加。- 因此,
c['MyTeam'] += 100 将在现有分数中增加100,如果密钥不存在,则设置c['MyTeam'] = 100 。
你只需要添加
1 2 | myDict[t1] = 0 myDict[t2] = 0 |
在你的for循环中