我只是Python的新手,所以我的代碼可能看起來不太好,但除了一件事外,它工作正常。 例如,如果我的listofnumberofpancakes是[1,2,3,3],並且正在查找最大值爲3,它將打印索引2和3.但是,稍後在我的代碼中,當我想要打印我列出的索引2和3的名稱的值,它將打印person2吃最多的煎餅,並在一個新的行3上吃最多的煎餅,我怎麼能把它們放在一起?像:person2和person3吃了最多的煎餅。 這裏是我的代碼:輸出的使用列表的多個索引來查找多個值的Python
pancakes
listofnames = []
listofnumberofpancakes = []
print "I will ask you ten times to give in the name of a person and how many pancakes he/she ate."
for count in range(0,10):
name = str(raw_input("What's the name of the person?"))
numberofpancakes = int(raw_input("And how many pancakes did he/she eat?"))
listofnames.append(name)
listofnumberofpancakes.append(numberofpancakes)
print "\n"
print "The max of pancakes eaten by a person was: ", max(listofnumberofpancakes)
print "The minimum of pancakes eaten by a person was: ", min(listofnumberofpancakes)
for i, j in enumerate(listofnumberofpancakes):
if j == max(listofnumberofpancakes):
print listofnames[i], "ate the most pancakes"
for lala, a in enumerate(listofnumberofpancakes):
if a == min(listofnumberofpancakes):
print listofnames[lala], "ate the least pancakes"
print listofnumberofpancakes
例子:
The max of pancakes eaten by a person was: 9
The minimum of pancakes eaten by a person was: 1
emma ate the most pancakes
tina ate the most pancakes
ina ate the least pancakes
[3, 3, 2, 5, 9, 9, 2, 1, 8, 4]
好的,謝謝你的幫助!我明白。 :) – Caeline