2011-08-18 93 views
0

我在列表中有一個列表,我嘗試遍歷一個列表,然後在內部列表中查找一個值,如果存在該值,列表中的變量。在Python中對多個列表進行迭代

這裏是我有,這似乎並沒有做這項工作:

for z, g in range(len(tablerows), len(andrewlist)): 
    tablerowslist = tablerows[z] 
    if "Andrew Alexander" in tablerowslist: 
     andrewlist[g] = tablerowslist 

任何想法?

這是列表結構:

[['Kyle Bazzy', 'FUP dropbox message', '8/18/2011', 'Swing Trade Stocks</a>', '&nbsp;', 'Affiliate blog'], ['Kyle Bazzy', 'FUP dropbox message', '8/18/2011', 'Swing Trade Software</a>', '&nbsp;', 'FUP from dropbox message. Affiliate blog'], ['Kyle Bazzy', 'FUP dropbox message', '8/18/2011', 'Start Day Trading (Blog)</a>', '&nbsp;', 'FUP from dropbox message'], ['Kyle Bazzy', 'Call, be VERY NICE', '8/18/2011', '&nbsp;', 'r24867</a>', 'We have been very nice to him, but he wants to cancel, we need to keep being nice and seeing what is wrong now.'], ['Jason Raznick', 'Reach out', '8/18/2011', 'Lexis Nexis</a>', '&nbsp;', '-'], ['Andrew Alexander', 'Check on account in one week', '8/18/2011', '&nbsp;', 'r46876</a>', '-'], ['Andrew Alexander', 'Cancel him from 5 dollar feed', '8/18/2011', '&nbsp;', 'r37693</a>', '-'], ['Aaron Wise', 'FUP with contract', '8/18/2011', 'YouTradeFX</a>', '&nbsp;', "Zisa is on vacation...FUP next week and then try again if she's still gone."], ['Aaron Wise', 'Email--JASON', '8/18/2011', 'Lexis Nexis</a>', '&nbsp;', 'email by today'], ['Sarah Knapp', '3rd FUP', '8/18/2011', 'Steven L. Pomeranz</a>', '&nbsp;', '-'], ['Sarah Knapp', 'Are we really interested in partnering?', '8/18/2011', 'Reverse Spins</a>', '&nbsp;', "V. political, doesn't seem like high quality content. Do we really want a partnership?"], ['Sarah Knapp', '2nd follow up', '8/18/2011', 'Business World</a>', '&nbsp;', '-'], ['Sarah Knapp', 'Determine whether we are actually interested in partnership', '8/18/2011', 'Fayrouz In Dallas</a>', '&nbsp;', "Hasn't updated since September 2010."], ['Sarah Knapp', 'See email exchange w/Autumn; what should happen', '8/18/2011', 'Graham and Doddsville</a>', '&nbsp;', "Wasn't sure if we could partner bc of regulations, but could do something meant simply to increase traffic both ways."], ['Sarah Knapp', '3rd follow up', '8/18/2011', 'Fund Action</a>', '&nbsp;', '-']] 

對於在它有一個特定值的任何值,比方說,安德魯·亞歷山大,我要讓這些單獨的列表。

例如:

[['Andrew Alexander', 'Check on account in one week', '8/18/2011', '&nbsp;', 'r46876</a>', '-'], ['Andrew Alexander', 'Cancel him from 5 dollar feed', '8/18/2011', '&nbsp;', 'r37693</a>', '-']] 
+0

你必須明確你的數據結構是什麼樣子。你只有兩個列表,還是你有一個列表,其中的每個元素是另一個列表?假設有多場比賽,你想收集所有的比賽嗎? –

+0

您可能正在尋找:'zip(範圍(len(tablerows)),範圍(len(andrewlist)))''。但仍然不明白代碼試圖做什麼。你應該舉一些例子輸入和輸出。 – utdemir

回答

1
>>> #I have a list within a list, 
>>> lol = [[1, 2, 42, 3], [4, 5, 6], [7, 42, 8]] 
>>> found = [] 
>>> #iterate through one list, 
>>> for i in lol: 
...  #in the inner list I want to search for a value 
...  if 42 in i: 
...   #if this value is present, place that list in a variable  
...   found.append(i) 
... 
>>> found 
[[1, 2, 42, 3], [7, 42, 8]] 
1

爲Z,G在範圍(LEN(tablerows),LEN(andrewlist)):

這意味着「使號碼的列表,其介於tablerows的長度和andrewlist的長度之間,然後依次查看這些數字中的每一個,並將這些數字視爲兩個值的列表,並將這兩個值分別指定爲zg,每次通過lo OP」。

一個數字不能被視爲兩個值的列表,因此失敗。

你需要做的更多,更清楚你在做什麼。在循環之前顯示tablerows的內容示例,以及循環之前的內容andrewlist以及之後的內容。你的描述是混亂的:我只能猜測,當你說「然後我想遍歷一個列表」時,你的意思是列表中的一個列表;但我不知道你是否想要一個特定的,或者每個人都需要。然後當你接下來說「然後在內部列表中我想......」時,我不知道你指的是什麼。

4

假設你有一個列表,其元素列表,這是我會怎麼做:

andrewlist = [row for row in tablerows if "Andrew Alexander" in row]