2017-09-18 21 views
0
#numerical_list is the list. 

for each in numerical_list: 
    x=each[0] 
    x1=each[1] 

    if x1 >= 1000: 

     thousand_or_greater.append(x) 

print (thousand_or_greater) 

有人能解釋一下這裏顯示的循環的名字? 是否有另一種替代解決方案創建一個新的字符串列表,只包含1000人以上共同

謝謝。

+0

首先,發佈我們的實際'numerical_list' – RomanPerekhrest

+0

哪一部分(S)的這個你懂不? –

+0

[List過濾:list comprehension vs. lambda +過濾器]的可能重複(https://stackoverflow.com/questions/3013449/list-filtering-list-comprehension-vs-lambda-filter) – Bilkokuya

回答

0

您遍歷某種形式的至少2個索引數據結構的列表,如果第二個指數大於或等於1000,你是第一個指標的值賦給列表

thousand_or_greater = [] 
numerical_list = [['Casey', 176544.328149], ['Riley', 154860.66517300002]] 

for each in numerical_list: 
    x=each[0] 
    x1=each[1] 
    if x1 >= 1000: 
     thousand_or_greater.append(x) 

print (thousand_or_greater) 
>> ['Casey', 'Riley'] 

這可以通過以下列表理解縮短:

[x[0] for x in numerical_list if x[1] >= 1000] 
>> ['Casey', 'Riley'] 
+0

[['Casey',176544.328149], ['Riley',154860.66517300002], 等是原始數值列表。它基本上顯示了名字,然後是有多少人擁有這個名字。問題是隻打印或顯示1000人以上使用的名稱。謝謝你的解釋。 – CheckEm

+0

如果數據結構是一個列表清單,並且您的原始解決方案適用於該問題,它應該仍然有效 – AK47

相關問題