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

          <bdo id='rwqS6'></bdo><ul id='rwqS6'></ul>

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

      2. <tfoot id='rwqS6'></tfoot>
      3. <legend id='rwqS6'><style id='rwqS6'><dir id='rwqS6'><q id='rwqS6'></q></dir></style></legend>

        强制对一个可迭代对象进行所有迭代

        Force all iterations on an iterable(强制对一个可迭代对象进行所有迭代)
              <tbody id='tuTdk'></tbody>
            • <bdo id='tuTdk'></bdo><ul id='tuTdk'></ul>
            • <legend id='tuTdk'><style id='tuTdk'><dir id='tuTdk'><q id='tuTdk'></q></dir></style></legend>
              <tfoot id='tuTdk'></tfoot>

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

                  本文介绍了强制对一个可迭代对象进行所有迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用 map 编写了一个 for 循环,其函数具有副作用.这是我的意思的一个最小的工作示例:

                  I've written a for-loop using map, with a function that has a side-effect. Here's a minimal working example of what I mean:

                  def someFunc(t):
                      n, d = t
                      d[n] = str(n)
                  
                  def main():
                      d = {}
                      map(somefunc, ((i,d) for i in range(10**3)))
                      print(len(d))
                  

                  因此很明显,someFunc 映射到 1000 以下的非负数,具有填充字典的副作用,该字典稍后用于其他用途.

                  So it's clear that someFunc, which is mapped onto the non-negative numbers under 1000, has the side-effect of populating a dictionary, which is later used for something else.

                  现在,鉴于上述代码的结构方式,print(len(d)) 的预期输出为 0,因为 map 返回一个迭代器,而不是一个列表(与 python2.x 不同).因此,如果我真的想查看应用于 d 的更改,那么我将不得不迭代该地图对象直到完成.我可以这样做的一种方法是:

                  Now, given the way that the above code has been structured, the expected output of print(len(d)) is 0, since map returns an iterator, and not a list (unlike python2.x). So if I really want to see the changes applied to d, then I would have to iterate over that map object until completion. One way I could do so is:

                  d = {}
                  for i in map(somefunc, ((i,d) for i in range(10**3))):
                      pass
                  

                  但这似乎不是很优雅.我可以在地图对象上调用 list ,但这需要 O(n) 内存,这是低效的.有没有办法强制对地图对象进行完整迭代?

                  But that doesn't seem very elegant. I could call list on the map object, but that would require O(n) memory, which is inefficient. Is there a way to force a full iteration over the map object?

                  推荐答案

                  你不想这样做(运行 map() 只是为了副作用),但是有一个 itertools consume 食谱 适用于此:

                  You don't want to do this (run a map() just for the side effects), but there is a itertools consume recipe that applies here:

                  from collections import deque
                  
                  deque(map(somefunc, ((i,d) for i in range(10**3))), maxlen=0)
                  

                  collections.deque() 对象,配置为最大大小为 0,消耗 map() 可迭代,不使用额外的内存.deque 对象是特别优化 对于这个用例.

                  The collections.deque() object, configured to a maximum size of 0, consumes the map() iterable with no additional memory use. The deque object is specifically optimized for this use-case.

                  这篇关于强制对一个可迭代对象进行所有迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

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

                  <legend id='UeVmG'><style id='UeVmG'><dir id='UeVmG'><q id='UeVmG'></q></dir></style></legend>

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

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