在Python中编写多个嵌套for循环的最佳方法是什么

在Python中编写多个嵌套for循环的最佳方法是什么,python,optimization,for-loop,Python,Optimization,For Loop,我正在编写一个代码,需要使用嵌套循环,如下所示: for r in range(m): for s in range(r+1, m): for t in range(s+1, m): for u in range(t+1, m): for v in range(u+1, m): arr.append([r,s,t,u,v]) 但是这种传统的嵌套循环看起来很难看。是否有一种方法可以用更少的线执行相同的

我正在编写一个代码,需要使用嵌套循环,如下所示:

for r in range(m):
   for s in range(r+1, m):
      for t in range(s+1, m):
         for u in range(t+1, m):
            for v in range(u+1, m):
                  arr.append([r,s,t,u,v])
但是这种传统的嵌套循环看起来很难看。是否有一种方法可以用更少的线执行相同的操作


我查看了itertools.product,但无法得到我想要的,因为我循环的所有开始/结束索引都取决于上一个级别。

您可以使用
itertools.compositions
,第二个参数是要执行的循环数

from itertools import combinations
for item in combinations("ABCD", 3):
    print item
输出

('A', 'B', 'C')
('A', 'B', 'D')
('A', 'C', 'D')
('B', 'C', 'D')
因此,通过列表理解,整个代码变成这样

[list(item) for item in combinations("ABCD", 3)]

您可以使用
itertools.compositions
,第二个参数是要执行的循环数

from itertools import combinations
for item in combinations("ABCD", 3):
    print item
输出

('A', 'B', 'C')
('A', 'B', 'D')
('A', 'C', 'D')
('B', 'C', 'D')
因此,通过列表理解,整个代码变成这样

[list(item) for item in combinations("ABCD", 3)]

直接从itertools.compositions创建阵列,例如使用n=8:

>>> from itertools import combinations
>>> arr = [i for i in combinations(range(8), 5)]
>>> arr
[(0, 1, 2, 3, 4), (0, 1, 2, 3, 5), (0, 1, 2, 3, 6), (0, 1, 2, 3, 7), (0, 1, 2, 4, 5), (0, 1, 2, 4, 6), (0, 1, 2, 4, 7), (0, 1, 2, 5, 6), (0, 1, 2, 5, 7), (0, 1, 2, 6, 7), (0, 1, 3, 4, 5), (0, 1, 3, 4, 6), (0, 1, 3, 4, 7), (0, 1, 3, 5, 6), (0, 1, 3, 5, 7), (0, 1, 3, 6, 7), (0, 1, 4, 5, 6), (0, 1, 4, 5, 7), (0, 1, 4, 6, 7), (0, 1, 5, 6, 7), (0, 2, 3, 4, 5), (0, 2, 3, 4, 6), (0, 2, 3, 4, 7), (0, 2, 3, 5, 6), (0, 2, 3, 5, 7), (0, 2, 3, 6, 7), (0, 2, 4, 5, 6), (0, 2, 4, 5, 7), (0, 2, 4, 6, 7), (0, 2, 5, 6, 7), (0, 3, 4, 5, 6), (0, 3, 4, 5, 7), (0, 3, 4, 6, 7), (0, 3, 5, 6, 7), (0, 4, 5, 6, 7), (1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 4, 7), (1, 2, 3, 5, 6), (1, 2, 3, 5, 7), (1, 2, 3, 6, 7), (1, 2, 4, 5, 6), (1, 2, 4, 5, 7), (1, 2, 4, 6, 7), (1, 2, 5, 6, 7), (1, 3, 4, 5, 6), (1, 3, 4, 5, 7), (1, 3, 4, 6, 7), (1, 3, 5, 6, 7), (1, 4, 5, 6, 7), (2, 3, 4, 5, 6), (2, 3, 4, 5, 7), (2, 3, 4, 6, 7), (2, 3, 5, 6, 7), (2, 4, 5, 6, 7), (3, 4, 5, 6, 7)]
>>> 
如果您要查找大量数据,最好编写一个生成器为您进行迭代,或者直接使用提供的生成器:

arr = [i for i in combinations(range(8), 5)]
for i in arr:
   # whatever you were going to use arr for....

直接从itertools.compositions创建阵列,例如使用n=8:

>>> from itertools import combinations
>>> arr = [i for i in combinations(range(8), 5)]
>>> arr
[(0, 1, 2, 3, 4), (0, 1, 2, 3, 5), (0, 1, 2, 3, 6), (0, 1, 2, 3, 7), (0, 1, 2, 4, 5), (0, 1, 2, 4, 6), (0, 1, 2, 4, 7), (0, 1, 2, 5, 6), (0, 1, 2, 5, 7), (0, 1, 2, 6, 7), (0, 1, 3, 4, 5), (0, 1, 3, 4, 6), (0, 1, 3, 4, 7), (0, 1, 3, 5, 6), (0, 1, 3, 5, 7), (0, 1, 3, 6, 7), (0, 1, 4, 5, 6), (0, 1, 4, 5, 7), (0, 1, 4, 6, 7), (0, 1, 5, 6, 7), (0, 2, 3, 4, 5), (0, 2, 3, 4, 6), (0, 2, 3, 4, 7), (0, 2, 3, 5, 6), (0, 2, 3, 5, 7), (0, 2, 3, 6, 7), (0, 2, 4, 5, 6), (0, 2, 4, 5, 7), (0, 2, 4, 6, 7), (0, 2, 5, 6, 7), (0, 3, 4, 5, 6), (0, 3, 4, 5, 7), (0, 3, 4, 6, 7), (0, 3, 5, 6, 7), (0, 4, 5, 6, 7), (1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 4, 7), (1, 2, 3, 5, 6), (1, 2, 3, 5, 7), (1, 2, 3, 6, 7), (1, 2, 4, 5, 6), (1, 2, 4, 5, 7), (1, 2, 4, 6, 7), (1, 2, 5, 6, 7), (1, 3, 4, 5, 6), (1, 3, 4, 5, 7), (1, 3, 4, 6, 7), (1, 3, 5, 6, 7), (1, 4, 5, 6, 7), (2, 3, 4, 5, 6), (2, 3, 4, 5, 7), (2, 3, 4, 6, 7), (2, 3, 5, 6, 7), (2, 4, 5, 6, 7), (3, 4, 5, 6, 7)]
>>> 
如果您要查找大量数据,最好编写一个生成器为您进行迭代,或者直接使用提供的生成器:

arr = [i for i in combinations(range(8), 5)]
for i in arr:
   # whatever you were going to use arr for....

有时这种算法通常可以使用递归方法实现。作为下一级迭代需要复杂测试的一个例子,考虑一个问题求解器:

for col0 in range(8):
    for col1 in range(8):
        if col1 != col0 and not same_diag(0, col0, 1, col1):
            for col2 in range(8):
                if (col2 != col1 and
                    col2 != col0 and
                    not same_diag(0, col0, 2, col0) and
                    not same_diag(1, col1, 2, col2)):
                    for col3 in range(8):
                        ... same pattern up to col7 ...
这种重复的代码可以用

def find_solution(i, free_cols, placed_queens):
    if i == 8:
        print_solution(placed_queens)
    else:
        for col in free_cols:
            if not any(same_diag(i, col, j, q)
                       for j, q in enumerate(placed_queens)):
                find_solution(i+1,
                              free_cols - set([col]),
                              placed_queens + [col])

您还可以得到级别数(即queens问题的电路板大小)成为一个参数。

有时这种算法通常可以使用递归方法实现。作为下一级迭代需要复杂测试的一个例子,考虑一个问题求解器:

for col0 in range(8):
    for col1 in range(8):
        if col1 != col0 and not same_diag(0, col0, 1, col1):
            for col2 in range(8):
                if (col2 != col1 and
                    col2 != col0 and
                    not same_diag(0, col0, 2, col0) and
                    not same_diag(1, col1, 2, col2)):
                    for col3 in range(8):
                        ... same pattern up to col7 ...
这种重复的代码可以用

def find_solution(i, free_cols, placed_queens):
    if i == 8:
        print_solution(placed_queens)
    else:
        for col in free_cols:
            if not any(same_diag(i, col, j, q)
                       for j, q in enumerate(placed_queens)):
                find_solution(i+1,
                              free_cols - set([col]),
                              placed_queens + [col])

您还可以得到级别数(即皇后问题的板大小)成为一个参数。

是的,我的坏只是读了标题,看到了你的答案,然后实际看到了他的答案code@JoelGreen好的:“USER 302444,欢迎你:”请考虑接受这个答案,如果它真的帮助你,是我的坏只是阅读标题,看到你的答案,然后实际上看到他的code@JoelGreen没关系:)@user3024444不客气:)如果真的帮助你,请考虑接受这个答案。