A = [1,2,0,0,3,4,5,-1,0,2,-1,-3,0,0,0,0,0,0,0,0,-2,-3,-4,-5,0,0,0] Return initial and ending index of longest sequence of 0’s in the list. As, longest sequence of 0’s in above list is 0,0,0,0,0,0,0,0 so it should return 12,19 as starting and ending index.Please help with some one line python code. I tried : which return 8 as the max length. Now, how to find start and end
Tag: list
Convert dictionary into list with length based on values
I have a dictionary I want to convert it to a list that holds the keys of the dictionary. Each key should be repeated as many times as its associated value. I’ve written this code that does the job: Output: But I would like it to be a list comprehension. How can this be done? Answer You can do it
Print list of lists in separate lines
I have a list of lists: I want the output in the following format: I have tried it the following way , but the outputs are not in the desired way: Outputs: While changing the print call to use end instead: Outputs: Any ideas? Answer Iterate through every sub-list in your original list and unpack it in the print call
Extend Python list by assigning beyond end (Matlab-style)
I want to use Python to create a file that looks like I tried to write this script An error is thrown: IndexError: list index out of range. There are many answers on similar questions that recommend using myParagraph.append(‘something’), which I know works. But I want to better understand the initialization of Python lists. How to manipulate a specific elements
Iterate over n successive elements of list (with overlapping)
The itertools python module implements some basic building blocks for iterators. As they say, “they form an iterator algebra”. I was expecting, but I could not find a succinctly way of doing the following iteration using the module. Given a list of ordered real numbers, for example … return a new list (or just iterate) grouping by some n value,
Retrieve length of slice from slice object in Python
The title explains itself, how to get 2 out of the object The documentation is somewhat confusing, or it is the wrong one https://docs.python.org/2/c-api/slice.html In particular I don’t understand what is the meaning of the output of One possible workaround is to slice a list with the slice object But this will fail for an arbitrary large slice. Thanks, I
Iterating over two lists one after another
I have two lists list1 and list2 of numbers, and I want to iterate over them with the same instructions. Like this: But that feels redundant. I know I can write for item in list1 + list2:, but it has a price of running-time. Is there a way do that without loose time? Answer This can be done with itertools.chain:
Appending each element of list at the end of file lines
I wrote this code.. However i can’t append elements at the end of line.. the output is just as this.. What changes does my code requires? Answer You can’t append to lines within a file without moving other data out of the way. If you can slurp the whole file into memory, this can be done without too much hassle
How to create a numpy array of lists?
I want to create a numpy array in which each element must be a list, so later I can append new elements to each. I have looked on google and here on stack overflow already, yet it seems nowhere to be found. Main issue is that numpy assumes your list must become an array, but that is not what I
Remove duplicate JSON objects from list in python
I have a list of dict where a particular value is repeated multiple times, and I would like to remove the duplicate values. My list: function to remove duplicate values: When I call this function I get generator object. When I try to iterate over the generator I get TypeError: unhashable type: ‘dict’ Is there a way to remove the