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

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

问题描述

我有一个整数列表,我正通过循环查找循环,以发现两个组合的元素是否等于另一个变量 t 。所以如果 t 等于 10 ,我有一个整数列表:
l = [1,2,3,4,5,8,9] ,那么函数应该打印所有不同的数字组合(1,9) code>,(2,8)



我觉得我快到了,发生在我使用 .pop()函数的列表中。下面的代码被用来显示所有需要计算的数字组合,但是列表中的其他元素都会被跳过。

  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)

以下是输出:

pre $ 1 $ 2
$ 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

注意 2 8 15 38 被跳过。
我正在使用 l.pop ,因此循环的第二个不会使用原始值,下一个迭代可以继续迭代列表中的下一个元素。

解决方案

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

看起来你想从列表中找到组合。您还可以尝试其他几种方法:


  • 您目前的做法最接近:使用 循环代替代替循环

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


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

     对于范围(len(l))中的i:
    对于范围内的k(i + 1,len(l)):
    print(l [i],l [k])


  • 或者使用 itertools.combinations / p>

      import itertools 
    for itertools.combinations(l,2)中的第一个,第二个:
    print(first ,第二)







然而,你可以做得比这更好。既然你正在寻找一对数字加起来的目标号码,只需要减去第一个目标来获得第二个,看看第二个数字是否在数字列表中。使用 set 来使这个查询在一段时间内发生,从而减少从O(n²)到O(n)的整体时间复杂度。

  numbers = set([1,2,5,8,13,15,26,38])
target = 10
for first数字:
秒=目标 - 第一个
如果第二个>第一个和第二个数字:
print(first,second)


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

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)

Here is the output:

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

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:

  • Closest to your current approach: Use a while loop instead of for loop

    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])
    

  • Or just use itertools.combinations

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


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)

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

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