简体   繁体   中英

How to remove a sub-list from a list?

I've got a big list of training_data with 50,000 samples. Every sample contains two sublists which contain two elements (sample, label).

I accidentally created another list inside the label item which has again two items.

ie)

                              main_list

                           _______|_______
                          |               |
                       sample           label(list)
                                     _____|_____
                                a(list)        b(list)

I want to remove 'b' sub-list from every example (50000). I hope I did explain it correctly. Save me.

如图所示,输出是50000个示例之一。我想从每个示例中删除突出显示的列表,以便每个示例都包含两个项目(第一个复杂数组以及0和1的列表)

If I understand correctly, try new_list = [ [x[0], x[1][0]] for x in main_list] . In any way, list comprehension will probably be enough to fix your problem...

I've tried to recreate your list hierarchy as follows:


a = ["w", "x"]
b = ["y", "z"]
label = [a, b]
sample = ["sample1", "sample2"]
main_list = [sample, label]

This gives us that

print(main_list)
[['sample1', 'sample2'], [['w', 'x'], [ 'y', 'z']]]

Then to remove b , you simply have to set

main_list = [main_list[0], main_list[1][0]] 

Does this achieve what you want?

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