Python split list at zeros [duplicate]

2024/11/16 17:59:43

Possible Duplicate:
Remove leading and trailing zeros from multidimensional list in Python

if I have a list such as:

my_list = [[1,2,0,1], [1,0,0,1]]

I want to split this at the zeros and throw them away, so that I end up with something like:

my_list = [[[1, 2], [1]], [[1],[1]]]

Any help much appreciated.

Answer

Divide an conquer, you can use a list comprehension to transform it to a simpler problem

def split_a_list_at_zeros(L):...return the_split_listmy_list = [split_a_list_at_zeros(item) for item in my_list]

and Akavall gives a great hint to split the list

from itertools import groupby
def split_a_list_at_zeros(L):return [list(g) for k, g in groupby(L, key=lambda x:x!=0) if k]
https://en.xdnf.cn/q/119167.html

Related Q&A

Creating Dynamic Text Using the blit method [duplicate]

This question already has answers here:How to display text with font and color using pygame?(7 answers)Closed last year.Im creating a basic game where a black square moves around the screen. There are…

Regex to match special list items

I have weird list of items and lists like this with | as a delimiters and [[ ]] as a parenthesis. It looks like this:| item1 | item2 | item3 | Ulist1[[ | item4 | item5 | Ulist2[[ | item6 | item7 ]] | i…

How to choose the best model dynamically using python

Here is my code im building 6 models and i am getting accuracy in that, how do i choose that dynamically which accuracy is greater and i want to execute only that model which as highest accuracy."…

How do you access specific elements from the nested lists

I am trying to access elements from the nested lists. For example, file = [[“Name”,”Age”,”Medal”,”Location”],[“Jack”,”31”,”Gold”,”China”],[“Jim”,”29”,”Silver”,”US”]]This data c…

Why does BLOCKCHAIN.COM API only return recipient BASE58 addresses and omits BECH32s?

Following this post, I am trying to access all transactions within the #630873 block in the bitcoin blockchain.import requestsr = requests.get(https://blockchain.info/block-height/630873?format=json) …

rename columns according to list

I have 3 lists of data frames and I want to add a suffix to each column according to whether it belongs to a certain list of data frames. its all in order, so the first item in the suffix list should b…

How to send a pdf file from Flask to ReactJS

How can I send a file from Flask to ReactJS? I have already code that in the frontend, the user upload a file and then that file goes to the Flask server, then in the flask server the file is modify, …

How to draw cover on each tile in memory in pygame

I am a beginner in pygame and I am not a English native speaker.My assignment is coding a game called Memory. This game contains 8 pairs pictures and an cover exists on each pictures. This week, our as…

Compare 2 Excel files and output an Excel file with differences

Assume for simplicity that the data files look like this, sorted on ID:ID | Data1 | Data2 | Data3 | Data4 199 | Tim | 55 | work | $55 345 | Joe | 45 | work | $34 356 | Sam |…

Problem to show data dynamically table in python flask

I want to show a table in the html using python flask framework. I have two array. One for column heading and another for data record. The length of the column heading and data record are dynamic. I ca…