Finding the longest list in given list that contains only positive numbers in Python [closed]

2024/11/19 0:35:45

I am newbie to Python just for a short time. Here are my codes, which seem a little bit clumsy and I want to learn how to optimize them just because of my curiosity. Would you mind recommending to me any other shorter solutions? That would be very generous and helpful. Thanks first.

A=[-1,2,-3,2,3,-4,3,4,5,-6,-19,-20,7,8,9,10,6,8,-19,-22,-99]front_count=0
start=0
front_position=0
end_position=0
length=0
step=0for x in A[start:]:if (x>=0):front_count= start+ A[start:].index(x)step= 0for y in A[front_count+1:]:if y>=0:step+=1else:breakif step>length:length=stepfront_position = front_countend_position = front_count + lengthprint("The longest list with positive numbers are", A[front_position:end_position+1])

The result will show up as:

The longest list with positive numbers are [7, 8, 9, 10, 6, 8]

I am expecting more other solutions to learn more about other syntax/tips/terms,.... that could be used to advance this code.

Answer

You can use itertools.groupby:

from itertools import groupbyA = [-1, 2, -3, 2, 3, -4, 3, 4, 5, -6, -19, -20, 7, 8, 9, 10, 6, 8, -19, -22, -99]max_l = []
for k, g in groupby(A, lambda k: k > 0):if k:max_l = max(max_l, list(g), key=len)print(max_l)

Prints:

[7, 8, 9, 10, 6, 8]
https://en.xdnf.cn/q/118607.html

Related Q&A

How to create multiple VideoCapture Objects

I wanted to create multiple VideoCapture Objects for stitching video from multiple cameras to a single video mashup.for example: I have path for three videos that I wanted to be read using Video Captur…

How to read Data from Url in python using Pandas?

I am trying to read the text data from the Url mentioned in the code. But it throws an error:ParserError: Error tokenizing data. C error: Expected 1 fields in line 4, saw 2url="https://cdn.upgrad.…

Testing multiple string in conditions in list comprehension [duplicate]

This question already has answers here:How to test multiple variables for equality against a single value?(31 answers)Closed 6 years ago.I am trying to add multiple or clauses to a python if statement…

Filter range from two dates in the same query Django/Python

I need the result from a query that filters two dates from the same model. I need to get in the result 5 days (today plus 4 days) from original date and sale from target date (today plus 4 more days) b…

Python While/For loop

how can I make this into a while loop and output the same thing????for x in range(56,120) :if (x < 57) :summation = 0summation = x + summationif (x == 119) :print (“Sum of integers from 56 to 1…

Read a file into a nested dictionary?

Say I have a simple file like so holding arbitrary values:A, 20, Monday, 14, Tuesday, 15, Tuesday, 16 B, 40, Wednesday, 14, Friday, 12How would I get it into a nested dictionary so that each k/v pair l…

Using .replace function

I have a code with more than 2500 lines that contains several references to GIS layers. I need to replace these layers in the code for several web maps so I have to find a way to automate a find and re…

Python Turtle unit of measurement

When we instantiate a turtle object, we can draw a circle. I wonder about the radius parameter of the circle() method. import turtle myTurtle = turtle.Turtle() myTurtle.circle(50)What is the unit of me…

Python reverse() vs [::-1] slice performance [duplicate]

This question already has answers here:Difference between reverse and [::-1](2 answers)Time complexity of reversed() in Python 3(1 answer)Closed last year.Python provides two ways to reverse a list: Li…

Django Callback on Facebook Credits

I would like to use Facebook Credits with my Django Application.In the Facebook Credits documentation, there is only a sample for the callback page in PHP (https://developers.facebook.com/blog/post/489…