【发布时间】:2019-02-28 08:27:57
【问题描述】:
我有以下列表:
a = [[1,2,3,4,5], [4,5,6,7,8], [1,2,3,4], [4,5,6,7,8,9], [2,3,4,5,6,7,8], [6,7,8,9], [5,6,7,8,9], [2,3,4,5,6], [3,4,5,6], [11,12,13,14,15], [13,14,15]]
用索引表示它们以便于理解:
0 [1, 2, 3, 4, 5]
1 [4, 5, 6, 7, 8]
2 [1, 2, 3, 4]
3 [4, 5, 6, 7, 8, 9]
4 [2, 3, 4, 5, 6, 7, 8]
5 [6, 7, 8, 9]
6 [5, 6, 7, 8, 9]
7 [2, 3, 4, 5, 6]
8 [3, 4, 5, 6]
9 [11, 12, 13, 14, 15]
10 [13, 14, 15]
我期待的输出将是一个元组列表,如下所示:
output = [(0,2,1), (3,1,1), (4,7,2), (4,1,2), (6,5,1), (3,5,2), (3,6,1), (7,8,1), (9,10,2)]
For example to explain first item of output i.e, (0,2,1):
0 ---> index of list under comparison with highest length
2 ---> index of list under comparison with lowest length
1 ---> difference in length of the two lists 0 & 2
现在,问题来了:
我的列表有相似的项目,在列表的开始或结束时长度相差一个和两个(或三个)。
我想对列表的索引及其作为元组的区别进行排序、分组、识别。
我检查了多个 stackoverflow 问题,但找不到类似的问题。
我是 python 新手,从以下代码开始,然后卡住了:
a = sorted(a, key = len)
incr = [list(g) for k, g in groupby(a, key=len)]
decr = list(reversed(incr))
ndecr = [i for j in decr for i in j]
for i in range(len(ndecr)-1):
if len(ndecr[i]) - len(ndecr[i+1]) == 1:
print(ndecr[i])
for i in range(len(ndecr)-2):
if len(ndecr[i]) - len(ndecr[i+2]) == 2:
print(ndecr[i])
for i in ndecr:
ele = i
ndecr.remove(i)
for j in ndecr:
if ele[:-1] == j:
print(j)
for i in ndecr:
ele = i
ndecr.remove(i)
for j in ndecr:
if ele[:-2] == j:
print(i)
请帮助我实现输出的方法。
【问题讨论】:
-
您到底想在输出中“排序”和“分组”什么?我想我了解您想要比较列表列表中的所有列表并输出比较列表的索引以及长度差异,但我不确定您希望如何对输出进行排序或分组。
标签: python arrays python-3.x list nested-lists