Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python列表中打印多个最大值(整数)?_Python_List_Max - Fatal编程技术网

如何在Python列表中打印多个最大值(整数)?

如何在Python列表中打印多个最大值(整数)?,python,list,max,Python,List,Max,我有一个投票程序,我需要找到投票的获胜者,或者找出投票最多的人之间是否有平局。我试图通过在列表中找到最大值(其中包含每个人获得的投票数),然后检查是否有其他值与之相等 使用的列表称为votesEach,它应该对应于candGroup(因此votesEach中的第一个值是候选人a的票数) 这是我的问题: 如果votesEach等于[1,0,0,0],则显示正确的内容(即候选人a获胜)。然而, 如果有多个赢家,则只显示其中一个(有时甚至显示错误的赢家)。例如,如果votesEach为[0,0,1,1

我有一个投票程序,我需要找到投票的获胜者,或者找出投票最多的人之间是否有平局。我试图通过在列表中找到最大值(其中包含每个人获得的投票数),然后检查是否有其他值与之相等

使用的列表称为votesEach,它应该对应于candGroup(因此votesEach中的第一个值是候选人a的票数)

这是我的问题: 如果votesEach等于[1,0,0,0],则显示正确的内容(即候选人a获胜)。然而, 如果有多个赢家,则只显示其中一个(有时甚至显示错误的赢家)。例如,如果votesEach为[0,0,1,1],则优胜者应为c和d,但a显示两次

可能是什么问题

这是我的密码:

candGroup = ['a', 'b', 'c', 'd']
votesEach = [0, 0, 1, 1]
winnercounter = 0
for i in votesEach:
    if i == max(votesEach):
        winnercounter += 1
if winnercounter == 1:
    winner = candGroup[votesEach.index(max(votesEach))]
    print(winner, 'has won the vote with', max(votesEach), 'votes')
else:
    print(votesEach)
    for i in votesEach:
        if i == max(votesEach):
            print(candGroup[votesEach[i]], 'has won the vote with', max(votesEach), 'votes')

试试这个简单的方法

candGroup = ['a', 'b', 'c', 'd']
votesEach = [0, 0, 1, 1]
# Zip function will zip two iterators and gives res in tuple like ('a',0),('b',0)..
# So by using those data we can filter out the winners by using maximum votes by creating a dictionary in a smart way
winners = {cand:votes for cand,votes in zip(candGroup,votesEach) if votes==max(votesEach)}
# Then here by iterating over each winners and getting the candidate name and votes
for candidate, votes in winners.items():
    print(candidate, 'has won the vote with', votes, 'votes')
输出:

c has won the vote with 1 votes
d has won the vote with 1 votes

首先找到最大投票数:

然后找到获胜者:


winners将是一个包含获奖者姓名的列表。

您的预期输出是什么?问题在最后一行,因为您将获得i的第一次出现的索引。我会尝试给出答案。@NanthakumarJJ我对votesEach的预期输出=[0,0,1,1]是否打印“c以1票赢得选票[新行]d以1票赢得选票”谢谢!你能解释一下最后三行是怎么工作的吗?我已经看过类似的东西好几次了,但不明白。是的,我会用explanation@noobprogrammer希望你能收到……谢谢!这是怎么回事?@noobprogrammer简单明了。首先找到最大票数的位置,然后将其映射到相应的候选人。
candGroup = ['a', 'b', 'c', 'd']
votesEach = [0, 0, 1, 1]

winningScore = max(votesEach)
winnersIndex = [index for index, value in enumerate(votesEach) if value == winningScore]

for i in winnersIndex:
    print(candGroup[i], "has won the vote with", winningScore, "votes" )
winners = [candGroup[i] for i in max_positions]
candGroup = ['a', 'b', 'c', 'd']
votesEach = [0, 0, 1, 1]

winningScore = max(votesEach)
winnersIndex = [index for index, value in enumerate(votesEach) if value == winningScore]

for i in winnersIndex:
    print(candGroup[i], "has won the vote with", winningScore, "votes" )