具有相同值的元组的子列表组
sublist groups of tuples with same values
我有以下列表:
o = [([1,1,1,1],[0.9,0.2,0.2,0.3]),([1,1,1],[0.7,0.6,0.7]),([1,1],[0.8,0.3]),([2,2,2,2,2],[0.8,0.9,0.2,0.3,0.1]),([2,2],[0.2,0.7]),([3,3,3],[0.9,0.4,0.3]),([3,3],[0.1,0.1]),([3,3,3,3],[0.9,0.4,0.3,0.1]),([4,4],[0.1,0.3]),([4,4],[0.6,0.7]),([4,4,4],[0.3,0.1,0.1])]
这是期望的输出:
output = [[([1,1,1,1],[0.9,0.2,0.2,0.3]),([1,1,1],[0.7,0.6,0.7]),([1,1],[0.8,0.3])],
[([2,2,2,2,2],[0.8,0.9,0.2,0.3,0.1]),([2,2],[0.2,0.7])],
[([3,3,3],[0.9,0.4,0.3]),([3,3],[0.1,0.1]),([3,3,3,3],[0.9,0.4,0.3,0.1])],
[([4,4],[0.1,0.3]),([4,4],[0.6,0.7]),([4,4,4],[0.3,0.1,0.1])]]
我想从初始列表创建子列表,按每个元组的第一个元素相等分组。
这是我到目前为止所做的,但我不知道如何获得如上所示的子列表。
x=[]
for i in range(len(o)-1):
if o[i][0][0] == o[i+1][0][0]:
x.append(o[i])
else:
x[-1].append(o[i])
任何帮助将不胜感激,谢谢!
x=[]
for i_o in range(len(o)):
for i_x in range(len(x)):
if o[i_o][0][0]==x[i_x][0][0][0]:
x[i_x].append(o[i_o])
break
else:
x.append([o[i_o]])
我有以下列表:
o = [([1,1,1,1],[0.9,0.2,0.2,0.3]),([1,1,1],[0.7,0.6,0.7]),([1,1],[0.8,0.3]),([2,2,2,2,2],[0.8,0.9,0.2,0.3,0.1]),([2,2],[0.2,0.7]),([3,3,3],[0.9,0.4,0.3]),([3,3],[0.1,0.1]),([3,3,3,3],[0.9,0.4,0.3,0.1]),([4,4],[0.1,0.3]),([4,4],[0.6,0.7]),([4,4,4],[0.3,0.1,0.1])]
这是期望的输出:
output = [[([1,1,1,1],[0.9,0.2,0.2,0.3]),([1,1,1],[0.7,0.6,0.7]),([1,1],[0.8,0.3])],
[([2,2,2,2,2],[0.8,0.9,0.2,0.3,0.1]),([2,2],[0.2,0.7])],
[([3,3,3],[0.9,0.4,0.3]),([3,3],[0.1,0.1]),([3,3,3,3],[0.9,0.4,0.3,0.1])],
[([4,4],[0.1,0.3]),([4,4],[0.6,0.7]),([4,4,4],[0.3,0.1,0.1])]]
我想从初始列表创建子列表,按每个元组的第一个元素相等分组。 这是我到目前为止所做的,但我不知道如何获得如上所示的子列表。
x=[]
for i in range(len(o)-1):
if o[i][0][0] == o[i+1][0][0]:
x.append(o[i])
else:
x[-1].append(o[i])
任何帮助将不胜感激,谢谢!
x=[]
for i_o in range(len(o)):
for i_x in range(len(x)):
if o[i_o][0][0]==x[i_x][0][0][0]:
x[i_x].append(o[i_o])
break
else:
x.append([o[i_o]])