简体   繁体   English

大多数pythonic(和有效)的方式将列表成对嵌套

[英]Most pythonic (and efficient) way of nesting a list in pairs

my list is: 我的清单是:

mylist=[1,2,3,4,5,6]

I would like to convert mylist into a list of pairs: 我想将mylist转换为对列表:

[[1,2],[3,4],[5,6]]

Is there a pythonic way of doing so? 有这样做的pythonic方式吗? List comprehension? 列表理解? Itertools? Itertools?

Yeppers, list comprehension is my usual way of doing it: Yeppers,list comprehension是我通常的做法:

>>> groupsize = 2
>>> [mylist[x:x+groupsize] for x in range(0,len(mylist),groupsize)]
[[1,2],[3,4],[5,6]]
>>> groupsize = 3
>>> [mylist[x:x+groupsize] for x in range(0,len(mylist),groupsize)]
[[1,2,3],[4,5,6]]

I use range for portability, if you are using python 2 (you probably are) change the range to xrange to save memory. 我使用range来实现可移植性,如果你使用python 2(你可能是)将range更改为xrange以节省内存。

My preferred technique: 我的首选技术:

>>> mylist = [1, 2, 3, 4, 5, 6]
>>> mylist = iter(mylist)
>>> zip(mylist, mylist)
[(1, 2), (3, 4), (5, 6)]

I usually use generators instead of lists anyway, so line 2 usually isn't required. 我通常使用生成器代替列表,因此通常不需要第2行。

An alternate way: 另一种方式:

zip( mylist[:-1:2], mylist[1::2] )

Which produces a list of tuples: 这会产生一个元组列表:

>>> zip(mylist[:-1:2],mylist[1::2])
[(1, 2), (3, 4), (5, 6)]

If you really want a list of lists: 如果你真的想要一个列表列表:

map(list, zip(mylist[:-1:2],mylist[1::2]))

Check out the "grouper" recipe from the itertools documentation : 查看itertools文档中的“grouper”配方:

def grouper(n, iterable, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

[mylist[2*n:2*n+2] for n in xrange(len(mylist)/2)]

This solution combines the use of list comprehensions and slicing to extract pairs in sequence from the original list, and build a list of the slices. 此解决方案结合使用列表推导和切片从原始列表中依次提取对,并构建切片列表。

Alternatively, [mylist[n:n+2] for n in xrange(0, len(mylist), 2)] which is the same except xrange counts by twos instead of the slices. 或者, [mylist[n:n+2] for n in xrange(0, len(mylist), 2)]除了xrange之外是相同的,而不是切片。 Thanks to Steven Rumbalski for the suggestion. 感谢Steven Rumbalski提出的建议。

And now for something completely different: here is a solution (ab)using zip and an ephemeral function instead of intermediate assignment: 现在有一些完全不同的东西:这是一个使用zip和短暂函数而不是中间赋值的解决方案(ab):

>>> (lambda i: zip(i, i))(iter(mylist))
[(1, 2), (3, 4), (5, 6)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 修复此列表的最pythonic /最有效的方法是什么? - Whats the most pythonic / efficient way to fix this list? python 中最有效的计算方式在对列表中查找对 - Most efficient computational way in python find pairs in a list of pairs 忽略带有括号的列表中的单词的最有效(pythonic)方法是什么? - What is the most efficient (pythonic) way to ignore words in a list that has parantheses? 从 Python 的列表中生成约束组合对的最有效方法 - Most efficient way of producing constrained pairs of combinations out of a list in Python 什么是最有效的pythonic方法: - Whats the most efficient pythonic way to do this : 从一组嵌套字典中创建键值对列表的大多数pythonic和最快的方法? - Most pythonic and fastest way to create a list of key value pairs from a set of nested dictionaries? 用Python方法将对列表转换为字典 - Pythonic way to transform list of pairs to dict 假设我在Python中有一个列表。 按部分将其随机化的最有效的Python方法是什么? - Suppose I had a list in Python. What's the most pythonic and efficient way to randomize it by sections? 从具有不同长度的字符串的二维列表创建数据帧的最 Pythonic/时尚/有效的方法 - Most pythonic/stylish/efficient way to create a dataframe from 2-dimensional list of string with varied length 在循环中创建NumPy数组的最有效,最Python方式 - Most efficient and most pythonic way to create a NumPy array within a loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM