Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将列表转换为元组列表?_Python_List_Tuples - Fatal编程技术网

Python 如何将列表转换为元组列表?

Python 如何将列表转换为元组列表?,python,list,tuples,Python,List,Tuples,我是Python新手,需要将列表转换为字典。我知道我们可以把元组列表转换成字典 这是输入列表: L = [1,term1, 3, term2, x, term3,... z, termN] 我想将此列表转换为元组列表(或直接转换为字典),如下所示: [(1, term1), (3, term2), (x, term3), ...(z, termN)] 我们如何在Python中轻松做到这一点 >>> L = [1, "term1", 3, "term2", 4, "term3

我是Python新手,需要将列表转换为字典。我知道我们可以把元组列表转换成字典

这是输入列表:

L = [1,term1, 3, term2, x, term3,... z, termN]
我想将此列表转换为元组列表(或直接转换为字典),如下所示:

[(1, term1), (3, term2), (x, term3), ...(z, termN)]
我们如何在Python中轻松做到这一点

>>> 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')]
您想一次将三个项目分组吗

>>> zip(it, it, it)
# 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)
是否要一次将N个项目分组

>>> zip(it, it, it)
# 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)

使用
zip
将连续的偶数和奇数元素配对,直接列出到字典中:

m = [ 1, 2, 3, 4, 5, 6, 7, 8 ] 
d = { x : y for x, y in zip(m[::2], m[1::2]) }
或者,由于您熟悉tuple->dict方向:

d = dict(t for t in zip(m[::2], m[1::2]))
甚至:


尝试使用组群集习惯用法:

zip(*[iter(L)]*2)
发件人:


iterables从左到右的求值顺序是有保证的。 这使得将一个数据系列聚类成一个习惯用法成为可能 n-使用zip(*[iter(s)]*n的长度组

使用切片

L = [1, "term1", 2, "term2", 3, "term3"]
L = zip(L[::2], L[1::2])

print L
试试这个

>>> 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')]
>>> 
或 或
以下代码将处理偶数和奇数大小的列表:

[set(L[i:i+2]) for i in range(0, len(L),2)]

Hey getting,
Traceback(最近一次调用):文件“ab.py”,第105行,打印[(L[i],L[i+1]),用于x范围内的i(0,len(L),2)]索引器:列表索引超出范围,根据你的问题,我认为你应该有偶数长度
L
,这将对Python2.X有效,在Python3.X中,你应该使用range而不是xrange@GrijeshChauhan我使用的是python2.7,所以我使用的是xrange。在python2中,xrange是一个生成器,range返回一个列表。对于大数字n,python2的范围(n)将消耗大量内存。在python3中,range是生成器,xrange不再存在。星飞是的,我理解这个问题,我只是想回应@trex,建议如何让它无误地执行。好的,我想我理解了你的代码,它与@thefourtheye的答案相同,只是你用
*2
复制了两份。我还可以写为
zip(*(iter(L),)*2)
@GrijeshChauhan是的,它是相同解决方案的缩写形式谢谢,非常有趣的是,您的代码只有17字节长:)非常优雅和惯用的解决方案。谢谢“保证iterables从左到右的评估顺序”对解释性引用进行投票。。尽管使用了很多次,我终于明白了它的工作原理!!每次“计算”迭代器时,它都“高级”到下一个元素。这就是为什么我们将下一个元素作为元组的第二个元素。现在很明显!对于dict,您可以简单地使用
dict(zip(*[iter(L)]*2))
有人能解释一下
zip
在这方面做了什么吗?我想我了解它是如何工作的,但显然不了解,因为每次访问迭代器时,它不会给出
[(1,1,1),('term1','term1','term1','term1'),…]
@user1717828。Zip将两件事配对,取自这里的同一个迭代器。如果您编写了
zip(iter(L)、iter(L)、iter(L))
它会这样做,但是通过中断iter的创建并共享它,您会得到这种行为。这在python2中是可行的,但在python3中,您实际上需要将
zip
对象放入
列表中,就像这样:
列表(zip(it,it))
因为python3不会计算
zip
,除非您明确要求它-如果您想在迭代时编辑项目,请查看一个好的解决方案!
>>> L = [1, "term1", 3, "term2", 4, "term3", 5, "termN"]
>>> map(None,*[iter(L)]*2)
[(1, 'term1'), (3, 'term2'), (4, 'term3'), (5, 'termN')]
>>> 
[set(L[i:i+2]) for i in range(0, len(L),2)]