简体   繁体   English

如何从列表中删除子列表?

[英]How to remove a sub-list from a list?

I've got a big list of training_data with 50,000 samples. 我有一份包含50,000个样本的training_data清单。 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). 我想从每个示例中删除“ b”子列表(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] . 如果我理解正确,请尝试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 然后要删除b ,只需设置

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

Does this achieve what you want? 这样能达到您想要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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