2011-08-18 70 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]