Python 查找列表上连续数字的序列
我有表格的清单Python 查找列表上连续数字的序列,python,python-3.x,list,Python,Python 3.x,List,我有表格的清单 [[143], [144, 210, 270], [145]] 具有任意数量的任意长度的子列表 我想构建一个函数,检查是否有连续(升序)数字序列,从每个列表中提取一个元素。序列的第一个元素应该在第一个列表中,第二个元素在第二个列表中,依此类推。如果序列存在,它应该返回序列。不需要找到每一个可能的序列。如果序列不存在,则返回'fail' 例如,在上面的示例中,函数应该输出一个列表[143144145] 我写了一个代码,但它只能部分工作。代码是 def pattern(exampl
[[143], [144, 210, 270], [145]]
具有任意数量的任意长度的子列表
我想构建一个函数,检查是否有连续(升序)数字序列,从每个列表中提取一个元素。序列的第一个元素应该在第一个列表中,第二个元素在第二个列表中,依此类推。如果序列存在,它应该返回序列。不需要找到每一个可能的序列。如果序列不存在,则返回'fail'
例如,在上面的示例中,函数应该输出一个列表[143144145]
我写了一个代码,但它只能部分工作。代码是
def pattern(example):
index = [0 for t in example]
stop = min([len(t) for t in example])
i=0
while i < stop:
attempt = [0 for t in example]
j = 0
i+1
while j < len(example):
attempt[j] = example[j][index[i]]
try:
if example[j+1][index[j+1]] - example[j][index[j]] == 1:
j+=1
index[j]+=1
else:
break
except:
break
if attempt[-1] != 0:
return attempt
else:
return 'fail'
def模式(示例):
索引=[0代表示例中的t]
停止=最小值([len(t)表示示例中的t])
i=0
当我停下来时:
尝试=[0代表示例中的t]
j=0
i+1
而j
它只适用于2个子列表,我不知道为什么。有帮助吗?使用
itertools.product
获取所有子列表的叉积,并测试其中是否有连续序列
import itertools as it
def pattern(example):
for l in it.product(*example):
if is_consecutive(l):
return l
return 'fail'
def is_consecutive(l):
return all(l[i] == l[i+1] - 1 for i in range(len(l)-1))
使用
itertools.product
获取所有子列表的叉积,并测试其中是否有连续序列
import itertools as it
def pattern(example):
for l in it.product(*example):
if is_consecutive(l):
return l
return 'fail'
def is_consecutive(l):
return all(l[i] == l[i+1] - 1 for i in range(len(l)-1))
通过在下一个列表中反复查找下一个编号
,可以大大简化代码
编辑:更正的版本
def find( list_of_lists ):
num = len(list_of_lists)
for start in list_of_lists[0]:
try:
print( "starting with", start )
next = start
last = start + num - 1
for list in list_of_lists[1:]:
next+=1
print( "looking for {} in {}".format(next, list))
if not next in list:
raise KeyError()
elif next == last:
return [x for x in range(start,next+1)]
except KeyError:
pass
return 'fail'
find(a)
starting with 1
looking for 2 in [3, 2, 6]
looking for 3 in [5, 7, 4]
starting with 3
looking for 4 in [3, 2, 6]
starting with 5
looking for 6 in [3, 2, 6]
looking for 7 in [5, 7, 4]
[5, 6, 7]
通过在下一个列表中反复查找下一个编号,可以大大简化代码
编辑:更正的版本
def find( list_of_lists ):
num = len(list_of_lists)
for start in list_of_lists[0]:
try:
print( "starting with", start )
next = start
last = start + num - 1
for list in list_of_lists[1:]:
next+=1
print( "looking for {} in {}".format(next, list))
if not next in list:
raise KeyError()
elif next == last:
return [x for x in range(start,next+1)]
except KeyError:
pass
return 'fail'
find(a)
starting with 1
looking for 2 in [3, 2, 6]
looking for 3 in [5, 7, 4]
starting with 3
looking for 4 in [3, 2, 6]
starting with 5
looking for 6 in [3, 2, 6]
looking for 7 in [5, 7, 4]
[5, 6, 7]
您可以使用chain
平铺列表
来自itertools导入链的
def find_continuous():
我的清单=[[143]、[144210270]、[145]]
展平列表=列表(链从列表(我的列表))
对于idx,枚举(展平列表)中的项:
当前=项目
保留的项目=[项目]
对于_idx,枚举中的_项(展平列表[idx+1:]):
如果当前+1==\u项:
当前=\u项
保留的\u项。追加(\u项)
如果len(保留项)>1:
退回保留的物品
返回“失败”
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
打印(f“连续:{find_contracted()}”)
输出:
consecutive: [143, 144, 145]
您可以使用chain
平铺列表
来自itertools导入链的
def find_continuous():
我的清单=[[143]、[144210270]、[145]]
展平列表=列表(链从列表(我的列表))
对于idx,枚举(展平列表)中的项:
当前=项目
保留的项目=[项目]
对于_idx,枚举中的_项(展平列表[idx+1:]):
如果当前+1==\u项:
当前=\u项
保留的\u项。追加(\u项)
如果len(保留项)>1:
退回保留的物品
返回“失败”
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
打印(f“连续:{find_contracted()}”)
输出:
consecutive: [143, 144, 145]
我喜欢在不使用导入的库作为学习体验的情况下尝试这一点
我采取了这种方法。首先,我简化了你的初始列表,所以我只有一个列表可以处理
然后我比较了列表中的项目,看看它们之间的差异是否为1。我遇到的问题是,我会得到我用dict函数删除的重复项(例如144个)
x = [[133], [144, 134, 136], [135]]
y = []
z = []
def isOne(itm, lst):
pos = 0
for stuff in lst:
sm = stuff - itm
if sm == 1:
return pos
else:
pos = pos + 1
return 0
for itms in x:
for itms2 in itms:
y.append(itms2)
for itms in y:
x = isOne(itms, y)
if x > 0:
z.append(itms)
z.append(y[x])
z = list(dict.fromkeys(z))
print(z)
我喜欢在不使用导入的库作为学习体验的情况下尝试这一点
我采取了这种方法。首先,我简化了你的初始列表,所以我只有一个列表可以处理
然后我比较了列表中的项目,看看它们之间的差异是否为1。我遇到的问题是,我会得到我用dict函数删除的重复项(例如144个)
x = [[133], [144, 134, 136], [135]]
y = []
z = []
def isOne(itm, lst):
pos = 0
for stuff in lst:
sm = stuff - itm
if sm == 1:
return pos
else:
pos = pos + 1
return 0
for itms in x:
for itms2 in itms:
y.append(itms2)
for itms in y:
x = isOne(itms, y)
if x > 0:
z.append(itms)
z.append(y[x])
z = list(dict.fromkeys(z))
print(z)
您可以使用函数chain()
:
您可以使用函数chain()
:
例如,如果我有这个列表[[1,3],[2,4],[3,5,6]]
。预期的结果是什么?它应该只需要每个列表中的一个元素并构建一个序列。在这种情况下,如果我有这个列表,[[1,3],[2,4],[3,5,6]
,那么输出将是[1,2,3]。预期的结果是什么?它应该只需要每个列表中的一个元素并构建一个序列。在这种情况下,输出为[1,2,3],谢谢您的回答。当输入为a=[[1,3,5]、[3,2,6]、[5,7,4]时,代码将失败。我稍微修改了代码,现在它可以工作了。我在返回“失败”块后添加了行if len(range(start,next+1))==len(list of_list):return[x代表range(start,next+1)中的x)]
。谢谢这是一个很好的解决方案。谢谢你的回答。当输入为a=[[1,3,5]、[3,2,6]、[5,7,4]时,代码将失败。我稍微修改了代码,现在它可以工作了。我在返回“失败”块后添加了行if len(range(start,next+1))==len(list of_list):return[x代表range(start,next+1)中的x)]
。谢谢这是一个很好的解决方案。这是一个很好且清晰的答案。谢谢这是一个很好的回答。谢谢