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

    <tfoot id='Ms5Z7'></tfoot>
      <bdo id='Ms5Z7'></bdo><ul id='Ms5Z7'></ul>
  • <legend id='Ms5Z7'><style id='Ms5Z7'><dir id='Ms5Z7'><q id='Ms5Z7'></q></dir></style></legend>

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

        什么是最“蟒蛇"?以块为单位迭代列表的方法?

        What is the most quot;pythonicquot; way to iterate over a list in chunks?(什么是最“蟒蛇?以块为单位迭代列表的方法?)

          <bdo id='qIMpf'></bdo><ul id='qIMpf'></ul>
        • <legend id='qIMpf'><style id='qIMpf'><dir id='qIMpf'><q id='qIMpf'></q></dir></style></legend>
        • <small id='qIMpf'></small><noframes id='qIMpf'>

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

                • 本文介绍了什么是最“蟒蛇"?以块为单位迭代列表的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 Python 脚本,它以整数列表作为输入,我需要一次处理四个整数.不幸的是,我无法控制输入,否则我会将其作为四元素元组列表传入.目前,我正在以这种方式对其进行迭代:

                  I have a Python script which takes as input a list of integers, which I need to work with four integers at a time. Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples. Currently, I'm iterating over it this way:

                  for i in range(0, len(ints), 4):
                      # dummy op for example code
                      foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]
                  

                  不过,它看起来很像C-think",这让我怀疑有一种更 Python 的方式来处理这种情况.该列表在迭代后被丢弃,因此不需要保留.也许这样的事情会更好?

                  It looks a lot like "C-think", though, which makes me suspect there's a more pythonic way of dealing with this situation. The list is discarded after iterating, so it needn't be preserved. Perhaps something like this would be better?

                  while ints:
                      foo += ints[0] * ints[1] + ints[2] * ints[3]
                      ints[0:4] = []
                  

                  还是不太感觉"对,不过.:-/

                  Still doesn't quite "feel" right, though. :-/

                  相关问题:怎么做你在 Python 中将列表分成大小均匀的块?

                  推荐答案

                  修改自itertools 文档的 >Recipes 部分:

                  from itertools import zip_longest
                  
                  def grouper(iterable, n, fillvalue=None):
                      args = [iter(iterable)] * n
                      return zip_longest(*args, fillvalue=fillvalue)
                  

                  示例

                  grouper('ABCDEFG', 3, 'x')  # --> 'ABC' 'DEF' 'Gxx'
                  

                  注意:在 Python 2 上使用 izip_longest 而不是 zip_longest.

                  Note: on Python 2 use izip_longest instead of zip_longest.

                  这篇关于什么是最“蟒蛇"?以块为单位迭代列表的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                  <i id='oidA8'><tr id='oidA8'><dt id='oidA8'><q id='oidA8'><span id='oidA8'><b id='oidA8'><form id='oidA8'><ins id='oidA8'></ins><ul id='oidA8'></ul><sub id='oidA8'></sub></form><legend id='oidA8'></legend><bdo id='oidA8'><pre id='oidA8'><center id='oidA8'></center></pre></bdo></b><th id='oidA8'></th></span></q></dt></tr></i><div id='oidA8'><tfoot id='oidA8'></tfoot><dl id='oidA8'><fieldset id='oidA8'></fieldset></dl></div>

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

                    <tbody id='oidA8'></tbody>

                  <tfoot id='oidA8'></tfoot>

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