Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
停止循环Python_Python_Loops_For Loop_Nested - Fatal编程技术网

停止循环Python

停止循环Python,python,loops,for-loop,nested,Python,Loops,For Loop,Nested,有没有办法在for循环中创建两个范围。我必须检查彼此之间的10个数组,但是如果选择了array1,它不应该检查自身。 如果选择了阵列1,则应使用阵列2-10检查阵列1。如果选择了第二个,则应使用阵列1和阵列3-10进行检查 我发现了一个链式函数,但它在我的情况下似乎不能正常工作,或者我做错了什么 for i in range (1,11): test_array is picked for j in chain(range(1,i),range(i+1,11)):

有没有办法在for循环中创建两个范围。我必须检查彼此之间的10个数组,但是如果选择了array1,它不应该检查自身。 如果选择了阵列1,则应使用阵列2-10检查阵列1。如果选择了第二个,则应使用阵列1和阵列3-10进行检查

我发现了一个链式函数,但它在我的情况下似乎不能正常工作,或者我做错了什么

for i in range (1,11): 
    test_array is picked 
    for j in chain(range(1,i),range(i+1,11)):
        does the check between test_array and all the other arrays Excluding the one picked as test_array

for i in range(1,11):
   pick test_array
       for j in range (1,11):
           if (j==i):
                 continue
             .... 
根据测试结果,这种和平与它自身相比是一种和平 上面的代码适用于2个for循环,但我嵌套了3个以上的for循环,使用continue它可以一直向下运行,这不是我想要的 谢谢

找到了我想要的答案:

for i in range(1,11):
    do something.
    for j in range(1,i) + range((i+1),11): 
        do something

使用
itertools.compositions()

您可以在此处使用:

输出:

[1, 2] ([3, 4], [4, 5], [5, 6])
[3, 4] ([1, 2], [4, 5], [5, 6])
[4, 5] ([1, 2], [3, 4], [5, 6])
[5, 6] ([1, 2], [3, 4], [4, 5])

还有另一种使用切片的方法:

lists = [[1, 2], [3,4], [4,5], [5,6]]
n = len(lists)
for i, item in enumerate(lists):
    rest = lists[:i] + lists[i+1:]
    print item, rest

现在还不清楚你到底在寻找什么,请提供一些例子。我想做一个从循环中排除某些数字的for循环。例如,如果选择了2,它应该是这样的(1,3,4,5,6,7,8,9,10)等等,希望这能让它更清楚为什么不添加
如果j==i:continue
[1, 2] ([3, 4], [4, 5], [5, 6])
[3, 4] ([1, 2], [4, 5], [5, 6])
[4, 5] ([1, 2], [3, 4], [5, 6])
[5, 6] ([1, 2], [3, 4], [4, 5])
lists = [[1, 2], [3,4], [4,5], [5,6]]
n = len(lists)
for i, item in enumerate(lists):
    rest = lists[:i] + lists[i+1:]
    print item, rest