简体   繁体   中英

remove first element from a list of sublist in an efficient way [python 3]

If I have a list like this:

my-list = [[[3, 1], [3, 0], [3, 0], [3, 1]], [[4, 1], [4, 0]]]

and I want to remove the first element of each "pair" of sublists like

my-list = [ [1,0,0,1] , [1,0] ] 

In the example above, the 3's and the 4's (first element) from the 2nd highest sublist are dropped. What is the most efficient way of achieving this?

Thank You!

You can use a nested list comprehension to work it out:

my_list = [[[3, 1], [3, 0], [3, 0], [3, 1]], [[4, 1], [4, 0]]]
new_list = [[x[1] for x in w] for w in my_list] 
print(new_list)

Output:

[[1, 0, 0, 1], [1, 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