在 python 中从 n 个连续列表中选取多个 n 个连续整数的最有效方法是什么,从每个列表中选取一个整数。这里的 n 相当大..比如说在 100s 的数量级。
L1 = [5,3,2,7,1]
L2 = [3,5,6,8,9,21,2]
L3 = [5,3,6,7,3,9]
我想从连续列表中打印出连续整数的范围,其中第一个元素从第一个列表中选取,第二个元素从第二个列表中选取,依此类推:
Candidate solution [5,6,7], [1,2,3], [7,8,9]
最佳答案
L1 = [5,3,2,7,1]
L2 = [3,5,6,8,9,21,2]
L3 = [5,3,6,7,3,9]
cons_l = []
L = [L2] + [L3] #+[L4] #+ ...+ ..... ### Add any number of list here..
j = 0
for l1 in L1:
cons_l.append([])
cons_l[j].append(l1)
for l in range(0, len(L)):
if l1+l+1 in L[l]:
cons_l[j].append(l1+l+1)
else:
del cons_l[j]
j -= 1
break
j += 1
print cons_l
关于python - 从python中的连续列表中识别连续数字组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36976419/