简体   繁体   中英

how to delete a 2D matrix row if first element of the row is 0 without using numpy in python

For example:

    a = [[0 0 0 ] [1 2 3] [1 2 1] [3 2 1] [2 0 1] [0 1 3]]

after deleting I want to obtain:

   a=[[1 2 3] [1 2 1] [3 2 1] [2 0 1]]

I want to only delete the row that has 0 as the 1st element.

Thank you,

You can do it with list comprehension. The 0 is already a boolean.

[r for r in a if r[0]]
for i, row in enumerate(a):
    if not row[0]:
        del a[i]

List comprehension in the docs at https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

a = [x for x in a if x[0] != 0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM