迭代Python中的多个列表

时间:2011-08-18 20:05:01

标签: python list

我在列表中有一个列表,我试图遍历一个列表,然后在内部列表中我想搜索一个值,如果该值存在,则将该列表放在一个变量中。 / p>

这就是我所拥有的,似乎没有做到这一点:

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>', '-']]

3 个答案:

答案 0 :(得分:4)

假设你有一个列表,其元素是列表,这就是我要做的事情:

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

答案 1 :(得分:1)

  

表示z,g表示范围(len(tablerows),len(andrewlist)):

这意味着“列出tablerows长度与andrewlist长度之间的数字列表,然后依次查看每个数字,并将这些数字视为两个值的列表,并且每次通过循环将这两个值分配给zg

不能将数字视为两个值的列表,因此失败。

你需要更清楚地了解自己在做什么。在循环之前显示tablerows的内容示例,在循环之前显示andrewlist的内容,以及之后应该是什么样子。你的描述很混乱:我只能猜到当你说“然后我想要遍历一个列表”时,你的意思是列表列表中的一个列表;但我不知道你是想要一个特定的,还是每一个。然后,当你接下来说“然后在内部列表中我想......”时,我不知道你指的是什么。

答案 2 :(得分: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]]