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

时间:2019-07-08 12:15:38

标签: python arrays list numpy

我有一个包含50,000个样本的training_data大列表。每个样本都包含两个子列表,其中包含两个元素(样本,标签)。

我不小心在标签项目内创建了另一个列表,该列表又有两个项目。

即)

                              main_list

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

我想从每个示例中删除“ b”子列表(50000)。希望我能正确解释。救救我。

As in the image, the output is one of the 50000 examples. And I want to remove the highlighted list from every example so that every example contains two items (the first complicated array and the list of 0s and 1s)

2 个答案:

答案 0 :(得分:2)

如果我理解正确,请尝试new_list = [ [x[0], x[1][0]] for x in main_list]。无论如何,列表理解可能足以解决您的问题...

答案 1 :(得分:0)

我尝试如下重新创建列表层次结构:


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

这给了我们

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

然后删除b,只需设置

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

这能达到您想要的吗?