You could if there is onle one max value
if find.count(max(find)) == 1:
and then use your old method (for single winter) or for
-loop to display all winners.
max_count = max(find)
for c in candidates:
if votes.count(c) == max_count:
print('MAX VOTES:', c)
Minimal working code
votes = ['A', 'B', 'A', 'Mr. X', 'B', 'C', 'D', 'C']
candidates = sorted(set(votes)) # create list of candidates base on votes
find = []
# display all candidates
print('--- candidates ---')
for c in candidates:
print(c)
# count votes and also display it
print('--- count votes ---')
for c in candidates:
count = votes.count(c)
find.append(count)
print(c, ':', count)
# find max count
print('--- max ---')
max_count = max(find)
print('max_count:', max_count)
print('
--- results ---
')
# check if only one max count
if find.count(max_count) == 1:
# display winner
index = find.index(max(find))
print('Winner:', votes[index])
else:
# display all
for c in candidates:
if votes.count(c) == max_count:
print('Draw:', c)
EDIT: x2
if find.count(max_count) == 1:
# ... code ...
else:
# display all
# --- before loop ---
all_winners = []
# --- loop ---
for c in candidates:
if votes.count(c) == max_count:
all_winners.append(c)
# --- after loop ---
print('Draw:', ",".join(all_winners) )
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…