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

    1. <small id='97uoe'></small><noframes id='97uoe'>

      <legend id='97uoe'><style id='97uoe'><dir id='97uoe'><q id='97uoe'></q></dir></style></legend>

        itertools.islice 与列表切片相比

        itertools.islice compared to list slice(itertools.islice 与列表切片相比)
        <i id='qyNBC'><tr id='qyNBC'><dt id='qyNBC'><q id='qyNBC'><span id='qyNBC'><b id='qyNBC'><form id='qyNBC'><ins id='qyNBC'></ins><ul id='qyNBC'></ul><sub id='qyNBC'></sub></form><legend id='qyNBC'></legend><bdo id='qyNBC'><pre id='qyNBC'><center id='qyNBC'></center></pre></bdo></b><th id='qyNBC'></th></span></q></dt></tr></i><div id='qyNBC'><tfoot id='qyNBC'></tfoot><dl id='qyNBC'><fieldset id='qyNBC'></fieldset></dl></div>

            <tbody id='qyNBC'></tbody>

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

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

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

                • 本文介绍了itertools.islice 与列表切片相比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在尝试应用一种算法来根据特定标准将 python 列表缩减为更小的列表.由于原始列表的体积很大,大约有 100k 个元素,我尝试使用 itertools 来避免多次内存分配,所以我想出了这个:

                  I've been trying to apply an algorithm to reduce a python list into a smaller one based on a certain criteria. Due to the large volume of the original list, in the order of 100k elements, I tried to itertools for avoiding multiple memory allocations so I came up with this:

                  reducedVec = [ 'F' if sum( 1 for x in islice(vec, i, i+ratio) if x == 'F' ) 
                                           > ratio / 3.0 else 'T'
                                  for i in xrange(0, len(vec), ratio) ]
                  

                  当 vec 有大约 100k 个元素时,执行此操作需要几分钟的时间,令人担忧.当我尝试改为:

                  Execution time for this takes a worryingly long time in the order of a few minutes, when vec has around 100k elements. When I tried instead:

                  reducedVec = [ 'F' if sum( 1 for x in vec[i:i+ratio] if x == 'F' ) 
                                           > ratio / 3.0 else 'T'
                                  for i in xrange(0, len(vec), ratio) ]
                  

                  本质上用切片替换islice,执行是即时的.

                  in essence replace islice with a slice the execution is instantaneous.

                  你能想出一个合理的解释吗?我原以为避免重复分配包含大量元素的新列表实际上会节省我几个计算周期,而不是削弱整个执行.

                  Can you think of a plausible explanation for this? I would have thought that avoiding to repeatedly allocate a new list with a substantial number of elements, would actually save me a few computational cycles instead of crippling the whole execution.

                  干杯,忒弥斯

                  推荐答案

                  islice 适用于任意迭代.为此,它必须遍历第一个 n-1 元素,而不是直接跳到第 n 个元素,将它们扔掉,然后生成你想要的元素.

                  islice works with arbitrary iterables. To do this, rather than jumping straight to the nth element, it has to iterate over the first n-1, throwing them away, then yield the ones you want.

                  从 itertools 文档中查看纯 Python 实现:

                  def islice(iterable, *args):
                      # islice('ABCDEFG', 2) --> A B
                      # islice('ABCDEFG', 2, 4) --> C D
                      # islice('ABCDEFG', 2, None) --> C D E F G
                      # islice('ABCDEFG', 0, None, 2) --> A C E G
                      s = slice(*args)
                      it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))
                      nexti = next(it)
                      for i, element in enumerate(iterable):
                          if i == nexti:
                              yield element
                              nexti = next(it)
                  

                  <小时>

                  谈到 itertools 文档,如果我尝试执行此操作,我可能会使用 grouper 配方.它实际上不会为您节省任何内存,但如果您将其重写为更懒,它可以,这并不难.


                  Speaking of the itertools documentation, if I was trying to do this operation, I'd probably use the grouper recipe. It won't actually save you any memory, but it could if you rewrote it to be lazier, which wouldn't be tough.

                  from __future__ import division
                  
                  from itertools import izip_longest
                  def grouper(n, iterable, fillvalue=None):
                      "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
                      args = [iter(iterable)] * n
                      return izip_longest(fillvalue=fillvalue, *args)
                  
                  reducedVec = []
                  for chunk in grouper(ratio, vec):
                      if sum(1 for x in chunk if x == 'F') > ratio / 3:
                          reducedVec.append('F')
                      else:
                          reducedVec.append('T')
                  

                  我喜欢使用 grouper 来抽象出连续的切片,发现这段代码比原来的代码更容易阅读

                  I like using grouper to abstract away the consecutive slices and find this code a lot easier to read than the original

                  这篇关于itertools.islice 与列表切片相比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 库)

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

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

                  <tfoot id='KDsK4'></tfoot>

                    <tbody id='KDsK4'></tbody>
                      <bdo id='KDsK4'></bdo><ul id='KDsK4'></ul>
                      • <legend id='KDsK4'><style id='KDsK4'><dir id='KDsK4'><q id='KDsK4'></q></dir></style></legend>