[英]find all sublist of list in python
I need to find maximum length of sublist with first element greater than last.我需要找到第一个元素大于最后一个元素的子列表的最大长度。
if a original list has first element greater than last then i just return the length of list.如果原始列表的第一个元素大于最后一个元素,那么我只返回列表的长度。 if not i have to find the sublist of list with maximum length and first element greater than last.如果不是,我必须找到最大长度和第一个元素大于最后一个元素的列表的子列表。
I written the coding for finding all sublist and checking condition.我编写了用于查找所有子列表和检查条件的代码。 My code takes more time for larger list to convert those into sublist.我的代码需要更多时间来将更大的列表转换为子列表。 i need to change this code more efficient.我需要更有效地更改此代码。 I think for method to get the combination of sublist takes more time.我认为获取子列表组合的方法需要更多时间。 anyone can provide me efficient way?任何人都可以为我提供有效的方法吗?
from itertools import combinations
def combos_with_exclusion(lst,length):
d=0
for combo in combinations((e for e in lst),length):
#yield list(combo)
#print (list(combo))
sublist=list(combo)
#print(sublist[0])
le=len(sublist)
if(sublist[0]>sublist[le-1]):
d=len(sublist)
if(d!=0):
break
return d
Here's a pretty straightforward solution.这是一个非常简单的解决方案。 It should be much faster than your solution :它应该比您的解决方案快得多:
l = [1, 21, 34, 55, 2, 12, 43, 54, 43]
n = len(l)
max_len = 1
max_list = l[0]
for i, x in enumerate(l):
for j in range(i + max_len, n):
y = l[j]
if x > y:
max_len = j - i + 1
max_list = l[i:j + 1]
print(max_len)
# 6
print(max_list)
# [55, 2, 12, 43, 54, 43]
You don't have to compute all the combinations first.您不必先计算所有组合。
Start with the whole list.从整个列表开始。 Then try all sublists of length "list_length - 1", then all sublists of length "list_length - 2", and so on.然后尝试所有长度为“list_length - 1”的子列表,然后尝试所有长度为“list_length - 2”的子列表,依此类推。
As soon as you get a list that meets the requirement, break out of the loop.一旦获得满足要求的列表,就跳出循环。 No need to try shorter lists.无需尝试较短的列表。
def max_length(input_list):
input_length = len(input_list)
for i in range(input_length):
for j in range(i + 1):
if input_list[j] > input_list[input_length - i - 1 + j]:
# return length
return input_length - i
# or return sublist, I'm not sure what you want
return input_list[j: input_length - i + j]
print(max_length([1,5,7,8,9,6,4,2,3,5,7,8,6,2]))
If I don't misunderstand your meaning,I tried to write some code,it might be helpful:如果我没有误解你的意思,我试着写了一些代码,它可能会有所帮助:
l1=[1,21,34,55,2,12,43,54,43]
l2=list(reversed(l1))
r=[]
for i in l1:
for j in l2:
if i>j:
sublist=l1[l1.index(i):len(l1)-l2.index(j)]
if sublist:
r.append(sublist)
break
print(r)
print(max([len(i) for i in r]))
Ouput:输出:
[[21, 34, 55, 2, 12], [34, 55, 2, 12], [55, 2, 12, 43, 54, 43], [54, 43]]
6
If I missed something,please tell me to improve it.Thanks.如果我错过了什么,请告诉我改进它。谢谢。
You van use list comprehensions:您使用列表推导式:
l=[1,21,34,55,2,12,43,54,43]
lst = [l[i:e+i+1] for i in range(len(l)) \
for e, x in enumerate(l[i:]) \
if x < l[i]]
print(max(lst, key=len))
#[55, 2, 12, 43, 54, 43]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.