2015-10-10 33 views
0

我只是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] 
+0

好的,謝謝你的幫助!我明白。 :) – Caeline

回答

1

當您完成循環時,您需要保存打印語句。它也有助於爲變量賦予有意義的名稱,並使用空行來邏輯分隔您的代碼。我刪除了代碼的交互部分以進行更快速的測試。

listofnames = ['alice', 'bob', 'charlie', 'dan'] 
listofnumberofpancakes = [1,2,3,3] 

top_eaters = [] 
for i, pcakes in enumerate(listofnumberofpancakes): 
    if pcakes == max(listofnumberofpancakes): 
     top_eaters += [listofnames[i]] 

bottom_feeders = [] # get it? it ryhmes with... nevermind 
for i, pcakes in enumerate(listofnumberofpancakes): 
    if pcakes == min(listofnumberofpancakes): 
     bottom_feeders += [listofnames[i]] 


print("The max of pancakes eaten by a person was: ", max(listofnumberofpancakes)) 
print("The minimum of pancakes eaten by a person was: ", min(listofnumberofpancakes)) 
print(' and '.join(top_eaters), "ate the most pancakes") 
print(' and '.join(bottom_feeders), "ate the least pancakes") 

然後代碼打印:

The max of pancakes eaten by a person was: 3 
The minimum of pancakes eaten by a person was: 1 
charlie and dan ate the most pancakes 
alice ate the least pancakes 
0

而不是在底部的打印名稱循環可以轉而添加這些名字分開列表和遍歷這些用一些邏輯打印:

maxList = [] 
minList = [] 

for i,j in enumerate(listofnumberofpancakes): 
    if j == max(listofnumberofpancakes): 
     maxList.append(listofnames[i]) 

if len(maxList) == 1: 
    print maxList[0] + " ate the most pancakes" 
else: 
    print " and ".join(maxList) + " ate the most pancakes" 

而且同樣的minList ...

「string」.join(list)將列表中的每個元素與作爲膠水的行爲內部提供的字符串連接起來。

+1

您實際上並不需要一個單獨的行來處理maxlist中只有一個項目。連接方法會自動解決這個問題(請參閱我的答案)。 – BlivetWidget

+0

我不知道。謝謝! – Nitro

+0

你打賭。這就是它如此強大的方法。 – BlivetWidget

0

您可以通過存儲所有的名字在名單最大值,然後將它們打印解決這個問題。你想打印任何其他的循環語句後

 

    if j == max(listofnumberofpancakes): 
     max_list.append(listofnames[i]) 

打印此列表:從本質上改變這一點:

 

    if j == max(listofnumberofpancakes): 
     print listofnames[i], "ate the most pancakes" 

到這樣的事情。您可以循環使用以特定格式打印。這是你的選擇。

相關問題