可能重复:
更改输出
这是代码:
def voting_borda(args):
results = {}
for sublist in args:
for i in range(0, 3):
if sublist[i] in results:
results[sublist[i]] += 3-i
else:
results[sublist[i]] = 3-i
winner = max(results, key=results.get)
return winner, results
print(voting_borda(
['GREEN','NDP', 'LIBERAL', 'CPC'],
['GREEN','CPC','LIBERAL','NDP'],
['LIBERAL','NDP', 'CPC', 'GREEN']
))
产生的输出是
"('GREEN', {'LIBERAL': 5, 'NDP': 4, 'GREEN': 6, 'CPC': 3})"
我不希望输出中的政党名称(自由党、ndp、green 和 cpc)我只需要这些值,如何编辑代码来实现这一点?
编辑:
测试上述代码后我收到的错误消息(带有: >>>voting_borda([['NDP', 'CPC', 'GREEN', 'LIBERAL'],['NDP', 'CPC', 'LIBERAL', '绿色'],['NDP','CPC','绿色','自由']])
回溯(最近一次通话):文件“”,第 1 行,在 vote_borda([['NDP', 'CPC', 'GREEN', 'LIBERAL'],['NDP', 'CPC', 'LIBERAL', 'GREEN'],['NDP', 'CPC', 'GREEN', 'LIBERAL']]) 文件“C:\Users\mycomp\Desktop\work\voting_systems.py”,第 144 行,在 vote_borda 中获胜者 = max (results, key=results.get) NameError: global name 'results' is not defined