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

时间:2013-02-26 18:05:22

标签: python

如何转换

[(212, u'comment', 0L), (205, u'main', 0L)]

进入这个?

(0L, 0L)

1 个答案:

答案 0 :(得分:4)

>>> data = [(212, u'comment', 0L), (205, u'main', 0L)]
>>> tuple(x[2] for x in data)
(0L, 0L)

或另一种方法......

>>> zip(*data)   # just showing what zip(*data) does
[(212, 205), (u'comment', u'main'), (0L, 0L)]
>>> zip(*data)[2]
(0L, 0L)

使用itertools.imap()operator.itemgetter() ...

>>> from itertools import imap
>>> from operator import itemgetter
>>> tuple(imap(itemgetter(2), data))
(0L, 0L)