python - 从python中的连续列表中识别连续数字组

标签 python algorithm list range continuous

在 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/

相关文章:

python - SQLAlchemy:是否可以在不绑定(bind) session 的情况下操作查询?

c++ - 生成二维非退化点集 - C++

java - 给定一个整数列表,其中一些可能是负数,提取总和为 2 个最大数字的对

c# - OOP 设计以及列表和集合 (C#)

python - 从 Python 中的文本文件导入 3-D 列表变量

python - 将字符串转换为分数列表

python - Pandas - 累积中位数

python - 在 theano 中使用多处理

python - 在opencv中使用霍夫变换检测垂直线

c++ - 代码优化子集总和