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

    1. <tfoot id='sFBc1'></tfoot>
      • <bdo id='sFBc1'></bdo><ul id='sFBc1'></ul>

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

      1. 有没有办法让 for-each 循环并行?

        Is there a way to have parallel for-each loops?(有没有办法让 for-each 循环并行?)

            • <bdo id='V7rEJ'></bdo><ul id='V7rEJ'></ul>

                  <tbody id='V7rEJ'></tbody>

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

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

                <legend id='V7rEJ'><style id='V7rEJ'><dir id='V7rEJ'><q id='V7rEJ'></q></dir></style></legend>
                  <tfoot id='V7rEJ'></tfoot>
                • 本文介绍了有没有办法让 for-each 循环并行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我在 Python 中有 2 个列表,我想并行遍历每个列表 - 例如对两个列表都使用元素 1,对两个列表使用元素 2……我知道我可以通过使用索引来做到这一点:

                  Let's say I have 2 lists in Python and I want to loop through each one in parallel - e.g. do something with element 1 for both lists, do something with element 2 for both lists... I know that I can do this by using an index:

                  for listIndex in range(len(list1)):
                     doSomething(list1[listIndex])
                     doSomething(list2[listIndex])
                  

                  但是有没有办法通过 foreach 循环更直观地做到这一点?类似于 for list1Value in list1, list2Value in list2...?

                  But is there a way to do this more intuitively, with a foreach loop? Something like for list1Value in list1, list2Value in list2...?

                  我目前在 Python 中遇到过这种情况,但这是一个长期存在的问题,我很想知道您是否可以用任何语言做到这一点.(我只是假设 Python 最有可能有处理这个问题的方法.)

                  I've currently run into this situation in Python, but this is a longstanding question and I'd be interested to know if you can do this in any language. (I just assumed that Python is the most likely to have a method of dealing with this.)

                  推荐答案

                  像这样?

                  for (a,b) in zip(list1, list2):
                    doSomething(a)
                    doSomething(b)
                  

                  虽然如果 doSomething() 没有进行 I/O 或更新全局状态,并且它一次只对其中一个元素起作用,但顺序并不重要,所以你可以使用 chain()(来自 itertools):

                  Though if doSomething() isn't doing I/O or updating global state, and it just works on one of the elements at a time, the order doesn't matter so you could just use chain() (from itertools):

                  for x in chain(list1, list2):
                    doSomething(x)
                  

                  恰到好处,from itertools import * 是我经常做的事情.考虑使用 izip() 而不是使用我上面给出的 zip().另请查看izip_longest()izip(count(), lst)等.欢迎使用函数式编程.:-)

                  Apropos, from itertools import * is something I do very often. Consider izip() instead of using the zip() I gave above. Also look into izip_longest(), izip(count(), lst), etc. Welcome to functional programming. :-)

                  哦,压缩也适用于更多列":

                  Oh, and zipping also works with more "columns":

                  for idx, a, b, c in izip(count(), A, B, C):
                    ...
                  

                  这篇关于有没有办法让 for-each 循环并行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

                    <tbody id='mCQ1Y'></tbody>
                    <bdo id='mCQ1Y'></bdo><ul id='mCQ1Y'></ul>

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

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