【发布时间】:2018-10-20 12:27:14
【问题描述】:
我有一个像这样的列表
[(1.0, 1.5), [2, 2], (1.5, 1.0), (1.1428571343421936, 0.28571426868438721), [1, 0], (0.5, 0.0), (0.66666668653488159, 0.0), [0, 0], [0, 1], (0.5, 1.25)]
我想通过将元组元素附加为子列表的第一个和最后一个元素来创建子列表,如下所示:
[[(1.0, 1.5), [2, 2], (1.5, 1.0)],[(1.1428571343421936,
0.28571426868438721), [1, 0], (0.5, 0.0)],[(0.66666668653488159, 0.0), [0, 0], [0, 1], (0.5, 1.25)]]
我尝试使用以下代码,但它似乎不起作用,因为我无法弄清楚如何以我想要的方式选择元组。它也给出了索引错误。
full_list = []
for ind,value in enumerate(flat_list):
if isinstance(value,(tuple)):
a = []
a.append(value)
temp = 0
while(temp!=1):
ind = ind + 1
j = flat_list[ind]
a.append(j)
if type(j) == 'tuple':
temp = 1
break
full_list.append(a)
else:
continue
print(full_list)
请提出一些建议!!
【问题讨论】:
标签: python python-2.7 list tuples