使用int迭代提取元组和覆盖

时间:2014-01-03 15:11:50

标签: python list

我有这个清单

list = [ [(6, 8, 9), 111], [(15,), 22], [(11, 20), 80] ]

我想提取元组并用0,1,2覆盖它们......以便我得到:

new_list = [[0, 1, 2], [3], [4, 5]]
         # 6,8,9 was overwritten with 0, 1, 2
         # 15    was overwritten with         3 
         # 11,20 was overwritten with           4, 5
         # ... 

我不需要列表[1]元素,如111,22和80。

有没有一种简单的方法可以做到这一点,我试图遍历它但是在正确的迭代中遇到了问题。

1 个答案:

答案 0 :(得分:4)

>>> from itertools import count
>>> lis = [ [(6, 8, 9), 111], [(15, ), 22], [(11, 20), 80] ]
>>> c = count(0)
#If you want a list of list then use: [[next(c) for _ in x[0]] for x in lis] 
>>> [tuple(next(c) for _ in x[0]) for x in lis]
[(0, 1, 2), (3,), (4, 5)]