从python中的连续列表中识别连续数字组 [英] Identify groups of continuous numbers from consecutive list in python

查看:115
本文介绍了从python中的连续列表中识别连续数字组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中从n个连续的列表中选择多个n个连续的整数,从每个列表中选择一个整数的最有效方法是什么.这里的n很大,大约是100秒.

What is the most efficient way in python for picking multiple n consecutive integers from n consecutive list, picking up one integer from each list. Here n is quite large..say in the order of 100s.

L1 = [5,3,2,7,1]
L2 = [3,5,6,8,9,21,2]
L3 = [5,3,6,7,3,9]

我想从连续列表中打印出连续整数的范围,其中第一个元素是从第一个列表中选取的,第二个元素是从第二个列表中选取的,依此类推:

I'd like to print out the ranges of consecutive integers from consecutive lists, where first element is picked up from first list, second element from second list and so on and so forth:

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中的连续列表中识别连续数字组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆