Python 如何将列表转换为元组列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23286254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a list to a list of tuples?
提问by
I am newbie to Python and need to convert a list to dictionary. I know that we can convert a list of tuples to a dictionary.
我是 Python 新手,需要将列表转换为字典。我知道我们可以将元组列表转换为字典。
This is the input list:
这是输入列表:
L = [1,term1, 3, term2, x, term3,... z, termN]
and I want to convert this list to a list of tuples (or directly to a dictionary) like this:
我想将此列表转换为元组列表(或直接转换为字典),如下所示:
[(1, term1), (3, term2), (x, term3), ...(z, termN)]
How can we do that easily in Python?
我们如何在 Python 中轻松做到这一点?
采纳答案by thefourtheye
>>> L = [1, "term1", 3, "term2", 4, "term3", 5, "termN"]
# Create an iterator
>>> it = iter(L)
# zip the iterator with itself
>>> zip(it, it)
[(1, 'term1'), (3, 'term2'), (4, 'term3'), (5, 'termN')]
You want to group three items at a time?
您想一次将三个项目分组吗?
>>> zip(it, it, it)
You want to group N items at a time?
您想一次对 N 个项目进行分组吗?
# Create N copies of the same iterator
it = [iter(L)] * N
# Unpack the copies of the iterator, and pass them as parameters to zip
>>> zip(*it)
回答by perreal
List directly into a dictionary using zip
to pair consecutive even and odd elements:
使用zip
配对连续偶数和奇数元素直接列出字典:
m = [ 1, 2, 3, 4, 5, 6, 7, 8 ]
d = { x : y for x, y in zip(m[::2], m[1::2]) }
or, since you are familiar with the tuple -> dict direction:
或者,由于您熟悉元组 -> dict 方向:
d = dict(t for t in zip(m[::2], m[1::2]))
even:
甚至:
d = dict(zip(m[::2], m[1::2]))
回答by Pablo Francisco Pérez Hidalgo
Try with the group clustering idiom:
尝试使用组聚类成语:
zip(*[iter(L)]*2)
From https://docs.python.org/2/library/functions.html:
从https://docs.python.org/2/library/functions.html:
The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n).
可迭代对象的从左到右的评估顺序是有保证的。这使得使用 zip(*[iter(s)]*n) 将数据系列聚类为 n 长度组的习语成为可能。
回答by Xing Fei
[(L[i], L[i+1]) for i in xrange(0, len(L), 2)]
回答by velis
Using slicing?
使用切片?
L = [1, "term1", 2, "term2", 3, "term3"]
L = zip(L[::2], L[1::2])
print L
回答by Nishant Nawarkhede
Try this ,
尝试这个 ,
>>> L = [1, "term1", 3, "term2", 4, "term3", 5, "termN"]
>>> it = iter(L)
>>> [(x, next(it)) for x in it ]
[(1, 'term1'), (3, 'term2'), (4, 'term3'), (5, 'termN')]
>>>
OR
或者
>>> L = [1, "term1", 3, "term2", 4, "term3", 5, "termN"]
>>> [i for i in zip(*[iter(L)]*2)]
[(1, 'term1'), (3, 'term2'), (4, 'term3'), (5, 'termN')]
OR
或者
>>> L = [1, "term1", 3, "term2", 4, "term3", 5, "termN"]
>>> map(None,*[iter(L)]*2)
[(1, 'term1'), (3, 'term2'), (4, 'term3'), (5, 'termN')]
>>>
回答by Sahil Chhabra
The below code will take care of both even and odd sized list :
下面的代码将处理偶数和奇数大小的列表:
[set(L[i:i+2]) for i in range(0, len(L),2)]