• <small id='i63Qk'></small><noframes id='i63Qk'>

  • <legend id='i63Qk'><style id='i63Qk'><dir id='i63Qk'><q id='i63Qk'></q></dir></style></legend>
    <i id='i63Qk'><tr id='i63Qk'><dt id='i63Qk'><q id='i63Qk'><span id='i63Qk'><b id='i63Qk'><form id='i63Qk'><ins id='i63Qk'></ins><ul id='i63Qk'></ul><sub id='i63Qk'></sub></form><legend id='i63Qk'></legend><bdo id='i63Qk'><pre id='i63Qk'><center id='i63Qk'></center></pre></bdo></b><th id='i63Qk'></th></span></q></dt></tr></i><div id='i63Qk'><tfoot id='i63Qk'></tfoot><dl id='i63Qk'><fieldset id='i63Qk'></fieldset></dl></div>

        <bdo id='i63Qk'></bdo><ul id='i63Qk'></ul>
      <tfoot id='i63Qk'></tfoot>

        可变长度嵌套循环Python

        Variable length nested loop Python(可变长度嵌套循环Python)
            <tbody id='RRXj4'></tbody>

            <i id='RRXj4'><tr id='RRXj4'><dt id='RRXj4'><q id='RRXj4'><span id='RRXj4'><b id='RRXj4'><form id='RRXj4'><ins id='RRXj4'></ins><ul id='RRXj4'></ul><sub id='RRXj4'></sub></form><legend id='RRXj4'></legend><bdo id='RRXj4'><pre id='RRXj4'><center id='RRXj4'></center></pre></bdo></b><th id='RRXj4'></th></span></q></dt></tr></i><div id='RRXj4'><tfoot id='RRXj4'></tfoot><dl id='RRXj4'><fieldset id='RRXj4'></fieldset></dl></div>

                <small id='RRXj4'></small><noframes id='RRXj4'>

                <legend id='RRXj4'><style id='RRXj4'><dir id='RRXj4'><q id='RRXj4'></q></dir></style></legend><tfoot id='RRXj4'></tfoot>
                  <bdo id='RRXj4'></bdo><ul id='RRXj4'></ul>
                • 本文介绍了可变长度嵌套循环Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在python中嵌套一个可变长度的for循环,以便能够重用变量。

                  for i in range(0,256):
                      for j in range(0,256):
                          for k in range(0,256):
                              ...
                              myvar[i][j][k]...
                  

                  在上面的代码中,有一个长度为3for的硬编码嵌套for循环。

                  应该是这样工作的:

                  for index[0] in range(0,256):
                    for index[1] in range(0,256):
                      for index[2] in range(0,256):
                        ...
                          for index[n-1] in range(0,256):
                            for index[n] in range(0,256):
                              myvar[index[0]][index[1]][index[2]] ... [index[n-1]][index[n]] ...
                  

                  对于任意值n

                  推荐答案

                  一种方法是对值的排列进行单循环。

                  考虑itertools.product的此行为:

                  >>> list(product(*[range(3)] * 2))
                  [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
                  

                  这样您就可以从一些迭代器的乘积上的一个循环中获得与从一堆嵌套循环中获得的值相同的值:

                  from itertools import product
                  
                  number = 3
                  ranges = [range(256)] * number
                  for values in product(*ranges):
                      ...
                  

                  这篇关于可变长度嵌套循环Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  GUI Freezes while downloading PyQt5 and Pytube(GUI在下载PyQt5和Pytube时冻结)
                  How to solve memory issues while multiprocessing using Pool.map()?(如何解决使用Pool.map()进行多处理时的内存问题?)
                  Python - How to use FastAPI and uvicorn.run without blocking the thread?(Python-如何使用FastAPI和uvicorn.run而不阻塞线程?)
                  How to increment a shared counter from multiple processes?(如何从多个进程递增共享计数器?)
                  Using pika, how to connect to rabbitmq running in docker, started with docker-compose with external network?(使用pika,如何连接运行在docker中的rabbitmq,从docker开始-与外部网络连接?)
                  How to use .rolling() on each row of a Pandas dataframe?(如何对 pandas 数据帧的每一行使用.roll()?)

                  <small id='Tr7ZQ'></small><noframes id='Tr7ZQ'>

                    <i id='Tr7ZQ'><tr id='Tr7ZQ'><dt id='Tr7ZQ'><q id='Tr7ZQ'><span id='Tr7ZQ'><b id='Tr7ZQ'><form id='Tr7ZQ'><ins id='Tr7ZQ'></ins><ul id='Tr7ZQ'></ul><sub id='Tr7ZQ'></sub></form><legend id='Tr7ZQ'></legend><bdo id='Tr7ZQ'><pre id='Tr7ZQ'><center id='Tr7ZQ'></center></pre></bdo></b><th id='Tr7ZQ'></th></span></q></dt></tr></i><div id='Tr7ZQ'><tfoot id='Tr7ZQ'></tfoot><dl id='Tr7ZQ'><fieldset id='Tr7ZQ'></fieldset></dl></div>
                      <legend id='Tr7ZQ'><style id='Tr7ZQ'><dir id='Tr7ZQ'><q id='Tr7ZQ'></q></dir></style></legend>
                        <tbody id='Tr7ZQ'></tbody>
                        <bdo id='Tr7ZQ'></bdo><ul id='Tr7ZQ'></ul>

                            <tfoot id='Tr7ZQ'></tfoot>