简体   繁体   English

python for循环在每次迭代后排除值

[英]python for loop excluding values after each iteration

so I have two python lists of ids with other info that I am looping through. 所以我有两个ID的python列表以及正在循环浏览的其他信息。 I try and match up the ids and assign row values based on the id join. 我尝试匹配ID,并根据ID联接分配行值。

the example below is just a generic test on what I am actually doing but once I understand how to solve what I need I will be able to apply it to my example. 下面的示例只是对我实际所做的一般测试,但是一旦我了解了如何解决需要的内容,我便可以将其应用于我的示例。

for row in l1:
    for info in l2:
        if row[0]==info[0]:
            row[1]=info[1]

each list respectively has about 11,000 rows in it so looping through the l1 then looping through every row in the l2 list to match up the ids takes a very long time. 每个列表中分别有大约11,000行,因此循环遍历l1然后循环遍历l2列表中的每一行以匹配id需要很长时间。 I was wondering if there is to say, okay since l1 id number 3 got iterated through and had the process finish for that id, then when the next row in l1 is being iterated l2 will now no NOT to even search for id number 3 which was processed already in the row before it. 我想知道是否可以这样说,因为l1 id 3遍历了,并完成了该id的处理,所以当l1的下一行被迭代时,l2现在将不再搜索3 id。已在之前的行中处理过。

I usually run a process like this in SQL, where it would sort of like a self join... 我通常在SQL中运行这样的过程,其中的过程有点像自我联接。

if somebody downvotes can they at least tell me why! 如果有人投票,他们至少可以告诉我原因!

Just elaborating on the previous answer, if you add break at the end of the if statement, once the if statement executes, it will stop the inside for loop for info in l2 只是详细说明上一个答案,如果在if语句的末尾添加break ,则一旦if语句执行,它将停止for info in l2的内部for循环

and continue with the outer for loop for row in l1 并继续for row in l1的外部for循环

if i'm not wrong , and got your question correctly, you can use loop flow control structures , like 如果我没记错,并且正确回答了您的问题,则可以使用循环流控制结构,例如

break

and

continue

for example in your case, you can break the inner loop when the match occurs and therefore your code won't waste more time looping through all l2. 例如,在您的情况下,您可以在匹配发生时中断内部循环,因此您的代码将不会浪费更多时间遍历所有l2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM