如何在Python中制作n维嵌套for循环?

时间:2018-06-14 15:04:50

标签: python loops dimensions

我有以下情况:

for x1 in range(x1, x2):
    for x2 in range(x3, x4):
        for x3 ...
            ...
                f(x1, x2, x3, ...)

如何将此转换为一种机制,我只告诉python使 n 嵌套循环,其中变量名是x1,x2,x3,x4,...?我当然不想手动编写所有可能性,因为可能存在很多维度。

4 个答案:

答案 0 :(得分:6)

您要做的是迭代产品。使用itertools.product

import itertools

ranges = [range(x1, x2), range(x3, x4), ...]

for xs in itertools.product(*ranges):
    f(*xs)

实施例

import itertools

ranges = [range(0, 2), range(1, 3), range(2, 4)]

for xs in itertools.product(*ranges):
    print(*xs)

输出

0 1 2
0 1 3
0 2 2
0 2 3
1 1 2
1 1 3
1 2 2
1 2 3

答案 1 :(得分:4)

推荐:itertools

对于与迭代相关的所有内容,

itertools是一个很棒的包:

from itertools import product

x1 = 3; x2 = 4
x3 = 0; x4 = 2
x5 = 42; x6 = 42
for x, y, z in product(range(x1, x2), range(x3, x4), range(x4, x5)):
    print(x, y, z)

给出

3 0 2
3 0 3
3 0 4
3 0 5
3 0 6
3 0 7
3 0 8
3 0 9
3 0 10
3 0 11
3 0 12
3 0 13
3 0 14
3 0 15
3 0 16
3 0 17
3 0 18
3 0 19
3 0 20
3 0 21
3 0 22
3 0 23
3 0 24
3 0 25
3 0 26
3 0 27
3 0 28
3 0 29
3 0 30
3 0 31
3 0 32
3 0 33
3 0 34
3 0 35
3 0 36
3 0 37
3 0 38
3 0 39
3 0 40
3 0 41
3 1 2
3 1 3
3 1 4
3 1 5
3 1 6
3 1 7
3 1 8
3 1 9
3 1 10
3 1 11
3 1 12
3 1 13
3 1 14
3 1 15
3 1 16
3 1 17
3 1 18
3 1 19
3 1 20
3 1 21
3 1 22
3 1 23
3 1 24
3 1 25
3 1 26
3 1 27
3 1 28
3 1 29
3 1 30
3 1 31
3 1 32
3 1 33
3 1 34
3 1 35
3 1 36
3 1 37
3 1 38
3 1 39
3 1 40
3 1 41

自己创建跨产品功能

def product(*args):
    pools = map(tuple, args)
    result = [[]]
    for pool in pools:
        result = [x + [y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

答案 2 :(得分:0)

您还可以使用numpy.ndindex来实现您的目标:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="../css/style.css" />
  <title>Floza Web Design | Conception web</title>
</head>
<body>
  <header>
    <div class="nav" id="nav">
      <a href="javascript:void(0)" id="close" class="close" onclick="closeNav()">&times</a>
      <a href="#">Accueil</a>
      <a href="#">Réalisations</a>
      <a href="#">Notre équipe</a>
      <a href="#">Nos services</a>
      <a href="#">Contact</a>
    </div>
    <span id="open" class="open" onclick="openNav()">&#9776;</span>
    <script type="text/javascript">
     function openNav(){
       document.getElementById('open').style.display = "none";
       document.getElementById('nav').style.width = "100%";
       }
     function closeNav(){
       document.getElementById('nav').style.width = "0";
       document.getElementById('open').style.display = "block";
     }
    </script>
</header>
  <div class="div"></div>


</body>
</html>

当你已经在脚本中使用numpy之类的东西时,这特别有用。您必须将'起点'(即x1,x2,...)添加到每个维度(如果它不是0)。

答案 3 :(得分:-1)

使用多处理!

import multiprocessing
from itertools import product

results = [] 
def callback(return_value):
    # this stores results
    results.append(return_value)

if __name__=="__main__" :
    pool = multiprocessing.Pool(4)
    args = product(range1, range2, range)
    for x, y, z in args:
        pool.apply_async(f,(x,y,z),call_back=callback)
    pool.close()
    pool.join()

现在您可以在充分利用机器的全部容量的同时运行您的功能!