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

    <small id='9TKnG'></small><noframes id='9TKnG'>

      <bdo id='9TKnG'></bdo><ul id='9TKnG'></ul>

      <legend id='9TKnG'><style id='9TKnG'><dir id='9TKnG'><q id='9TKnG'></q></dir></style></legend>

        在 Python 中以列表(循环方式)迭代对

        Iterate over pairs in a list (circular fashion) in Python(在 Python 中以列表(循环方式)迭代对)
        • <bdo id='IpqeP'></bdo><ul id='IpqeP'></ul>

                <tbody id='IpqeP'></tbody>

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

                <i id='IpqeP'><tr id='IpqeP'><dt id='IpqeP'><q id='IpqeP'><span id='IpqeP'><b id='IpqeP'><form id='IpqeP'><ins id='IpqeP'></ins><ul id='IpqeP'></ul><sub id='IpqeP'></sub></form><legend id='IpqeP'></legend><bdo id='IpqeP'><pre id='IpqeP'><center id='IpqeP'></center></pre></bdo></b><th id='IpqeP'></th></span></q></dt></tr></i><div id='IpqeP'><tfoot id='IpqeP'></tfoot><dl id='IpqeP'><fieldset id='IpqeP'></fieldset></dl></div>
              1. <tfoot id='IpqeP'></tfoot>
                  <legend id='IpqeP'><style id='IpqeP'><dir id='IpqeP'><q id='IpqeP'></q></dir></style></legend>
                  本文介绍了在 Python 中以列表(循环方式)迭代对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  问题很简单,我想迭代列表的每个元素和下一个成对的元素(用第一个包裹最后一个).

                  The problem is easy, I want to iterate over each element of the list and the next one in pairs (wrapping the last one with the first).

                  我想到了两种非pythonic的方法:

                  I've thought about two unpythonic ways of doing it:

                  def pairs(lst):
                      n = len(lst)
                      for i in range(n):
                          yield lst[i],lst[(i+1)%n]
                  

                  和:

                  def pairs(lst):
                      return zip(lst,lst[1:]+[lst[:1]])
                  

                  预期输出:

                  >>> for i in pairs(range(10)):
                      print i
                  
                  (0, 1)
                  (1, 2)
                  (2, 3)
                  (3, 4)
                  (4, 5)
                  (5, 6)
                  (6, 7)
                  (7, 8)
                  (8, 9)
                  (9, 0)
                  >>> 
                  

                  有什么关于更 Pythonic 的方法的建议吗?也许有一个我没有听说过的预定义函数?

                  any suggestions about a more pythonic way of doing this? maybe there is a predefined function out there I haven't heard about?

                  还有一个更通用的 n-fold(使用三重奏、四重奏等,而不是对)版本可能会很有趣.

                  also a more general n-fold (with triplets, quartets, etc. instead of pairs) version could be interesting.

                  推荐答案

                  我自己编写了元组通用版本,我喜欢第一个,因为它优雅简洁,我越看越觉得 Pythonic我......毕竟,有什么比一个带有 zip、星号参数扩展、列表推导、列表切片、列表连接和范围"的单行更 Pythonic 的?

                  I've coded myself the tuple general versions, I like the first one for it's ellegant simplicity, the more I look at it, the more Pythonic it feels to me... after all, what is more Pythonic than a one liner with zip, asterisk argument expansion, list comprehensions, list slicing, list concatenation and "range"?

                  def ntuples(lst, n):
                      return zip(*[lst[i:]+lst[:i] for i in range(n)])
                  

                  即使对于大型列表,itertools 版本也应该足够高效...

                  The itertools version should be efficient enough even for large lists...

                  from itertools import *
                  def ntuples(lst, n):
                      return izip(*[chain(islice(lst,i,None), islice(lst,None,i)) for i in range(n)])
                  

                  还有一个用于不可索引序列的版本:

                  And a version for non-indexable sequences:

                  from itertools import *
                  def ntuples(seq, n):
                      iseq = iter(seq)
                      curr = head = tuple(islice(iseq, n))
                      for x in chain(iseq, head):
                          yield curr
                          curr = curr[1:] + (x,)
                  

                  无论如何,谢谢大家的建议!:-)

                  Anyway, thanks everybody for your suggestions! :-)

                  这篇关于在 Python 中以列表(循环方式)迭代对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  Python - Initializing Multiple Lists/Line(Python - 初始化多个列表/行)
                  Python: Why quot;returnquot; won#180;t print out all list elements in a simple for loop and quot;printquot; will do it?(Python:为什么“回归?不会在简单的 for 循环和“打印中打印出所有列表元素.会做的?)
                  Return middle node of linked list with recursion(递归返回链表的中间节点)
                  Return items from list in function. Python(从函数列表中返回项目.Python)
                  Why does quot;return list.sort()quot; return None, not the list?(为什么“返回 list.sort()?返回无,而不是列表?)

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

                        <bdo id='09uxU'></bdo><ul id='09uxU'></ul>
                              <tbody id='09uxU'></tbody>