在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>', ' ', 'Affiliate blog'], ['Kyle Bazzy', 'FUP dropbox message', '8/18/2011', 'Swing Trade Software</a>', ' ', 'FUP from dropbox message. Affiliate blog'], ['Kyle Bazzy', 'FUP dropbox message', '8/18/2011', 'Start Day Trading (Blog)</a>', ' ', 'FUP from dropbox message'], ['Kyle Bazzy', 'Call, be VERY NICE', '8/18/2011', ' ', '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>', ' ', '-'], ['Andrew Alexander', 'Check on account in one week', '8/18/2011', ' ', 'r46876</a>', '-'], ['Andrew Alexander', 'Cancel him from 5 dollar feed', '8/18/2011', ' ', 'r37693</a>', '-'], ['Aaron Wise', 'FUP with contract', '8/18/2011', 'YouTradeFX</a>', ' ', "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>', ' ', 'email by today'], ['Sarah Knapp', '3rd FUP', '8/18/2011', 'Steven L. Pomeranz</a>', ' ', '-'], ['Sarah Knapp', 'Are we really interested in partnering?', '8/18/2011', 'Reverse Spins</a>', ' ', "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>', ' ', '-'], ['Sarah Knapp', 'Determine whether we are actually interested in partnership', '8/18/2011', 'Fayrouz In Dallas</a>', ' ', "Hasn't updated since September 2010."], ['Sarah Knapp', 'See email exchange w/Autumn; what should happen', '8/18/2011', 'Graham and Doddsville</a>', ' ', "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>', ' ', '-']]
对于任何包含特定值的项,比如说,Andrew Alexander,我想把这些项单独列出来。
举个例子:
[['Andrew Alexander', 'Check on account in one week', '8/18/2011', ' ', 'r46876</a>', '-'], ['Andrew Alexander', 'Cancel him from 5 dollar feed', '8/18/2011', ' ', 'r37693</a>', '-']]
3 个回答
for z, g in range(len(tablerows), len(andrewlist)):
这段话的意思是“创建一个数字列表,这些数字在tablerows
的长度和andrewlist
的长度之间,然后依次查看这些数字,把它们当作两个值的列表,每次循环时把这两个值分别赋给z
和g
。”
但是,一个数字不能被当作两个值的列表,所以这样做会出错。
你需要更加清楚地表达你想要做的事情。在循环之前,展示一下tablerows
的内容和andrewlist
的内容,以及循环之后应该是什么样子。你的描述有些混乱:我只能猜测当你说“然后我想遍历一个列表”时,你是指你那个包含多个列表的列表中的某一个;但我不确定你是想要一个特定的,还是想要一个接一个地遍历每一个。接下来当你说“然后在内部列表中我想要...”时,我完全不知道你在指什么。
假设你有一个列表,里面的元素也是列表,那么我会这样做:
andrewlist = [row for row in tablerows if "Andrew Alexander" in row]
在编程中,有时候我们会遇到一些问题,特别是在使用某些工具或库的时候。这些问题可能会让我们感到困惑,尤其是当我们刚开始学习编程的时候。为了帮助大家更好地理解这些问题,很多人会在网上提问,比如在StackOverflow上。
在这些提问中,通常会有人描述他们遇到的具体情况,可能是代码出错了,或者某个功能没有按预期工作。他们会提供一些代码示例,像
>>> #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]]
这样的占位符,表示他们的代码片段。这些代码片段可以帮助其他人更好地理解问题所在。
总之,提问和分享代码是学习编程的重要部分,能够帮助我们更快地解决问题,提升自己的技能。