为什么我的 for 循环跳过列表中的元素? [英] Why is my for loop skipping an element in my list?

查看:21
本文介绍了为什么我的 for 循环跳过列表中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数列表,我正在通过 for 循环运行它以发现两个元素的总和是否等于另一个变量 t.因此,如果 t 等于 10 并且我有一个整数列表:l = [1,2,3,4,5,8,9],那么函数应该打印所有不同的数字组合(1,9), <代码>(2,8).

I have a list of integers which I am running through a for-loop to discover if the sum of two of the elements equals another variable t. So if t was equal to 10 and I had a list of integers: l = [1,2,3,4,5,8,9], then the function should print all the different combinations of numbers (1,9), (2,8).

我觉得我快到了,但是当我使用 .pop() 函数时,列表发生了一些奇怪的事情.下面的代码用于显示需要计算的所有数字组合,但跳过列表中的所有其他元素.

I feel I am almost there, but something strange happens to the list when I use the .pop() function. The code below is being used to show all the combinations of numbers which need to be calculated, but every other element in the list is skipped.

l = [1,2,5,8,13,15,26,38]
c = 10
for i in l:
    first = i
    l.pop(0)
    for x in l:
        second = x
        print(first,second)

这是输出:

1 2
1 5
1 8
1 13
1 15
1 26
1 38
5 5
5 8
5 13
5 15
5 26
5 38
13 8
13 13
13 15
13 26
13 38
26 13
26 15
26 26
26 38

注意如何跳过 281538.我正在使用 l.pop() 以便第二个 for 循环不会使用原始值,然后下一次迭代可以继续迭代中的下一个元素清单.

Notice how the 2, 8, 15, and 38 are skipped. I am using l.pop() so that the second for-loop will not use the original value, and the next iteration can then go on to iterate the next element in the list.

推荐答案

您尝试执行的操作不起作用,因为您在迭代列表时正在修改它.假设当前的指针"指向第一个元素.现在你弹出第一个,所以指针在第二个.但是当循环进行时,指针移动到第三个,第二个被跳过.

What you are trying to do will not work, as you are modifying the list while you are iterating it. Say the current "pointer" points to the first element. Now you pop the first, so the pointer is at the second. But when the loop advances, the pointer is moved to the third, and the second is skipped.

您似乎想从列表中查找组合.您还可以尝试其他几种方法:

It seems you want to find combinations from a list. There are a few other ways you can try:

  • 最接近您当前的方法:使用 while 循环而不是 for 循环

while l:
    first = l.pop(0)
    for second in l:
        print(first, second)

  • 或者你可以只迭代索引而不是列表本身:

  • Or you could just iterate the indices instead of the lists themselves:

    for i in range(len(l)):
        for k in range(i+1, len(l)):
            print(l[i], l[k])
    

  • 或者只是使用 itertools.combinations

    import itertools
    for first, second in itertools.combinations(l, 2):
        print(first, second)
    

  • 但是,您可以做得更好.由于您正在寻找与某个目标数字相加的一对数字,只需从目标中减去第一个数字即可获得第二个数字,然后查看第二个数字是否在数字列表中.使用 set 使这种查找在恒定时间内发生,从而将整体时间复杂度从 O(n²) 降低到 O(n).

    However, you can do better than that. Since you are looking for a pair of numbers that adds up to some target number, just subtract the first from the target to get the second and see if that second number is in the list of numbers. Use a set to make this lookup happen in constant time, reducing your overall time complexity from O(n²) to O(n).

    numbers = set([1,2,5,8,13,15,26,38])
    target = 10
    for first in numbers:
        second = target - first
        if second > first and second in numbers:
            print(first, second)
    

    这篇关于为什么我的 for 循环跳过列表中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆