如何从python中的列表列表中删除列表元素

时间:2022-08-01 21:42:28

I tried to remove the third and the fourth list from the list of list in python.

我试图从python中的列表列表中删除第三个和第四个列表。

My list of list is below:

我的清单如下:

List =  [
            ['101', 'Dashboard', '1', '1'],
            ['102', 'Potential Cstomer', '1', '1'],
            ['102-01', 'Potential Cstomer', '1', '1'],
            ['102-02', 'Potential Cstomer Activity', '1', '1']
        ]

After remove the third and fourth element of list, I would like to be like this:

删除列表的第三和第四个元素后,我想这样:

NewList =  [
            ['101', 'Dashboard'],
            ['102', 'Potential Cstomer'],
            ['102-01', 'Potential Cstomer'],
            ['102-02', 'Potential Customer Activity']
        ]

I tried my code like below but it did not make any change.

我尝试了下面的代码,但它没有做任何改变。

    NewList     = [list(element) for element in List if element[0] or element[1]]

    print NewList

How should I change my current code to achieve my expected result? Thanks.

我应该如何更改当前代码以实现预期结果?谢谢。

3 个解决方案

#1


5  

Slice each nested list in a list comprehension. The slice notation starts at index 0 and stops at 1 i.e. [0, 2):

在列表推导中切片每个嵌套列表。切片表示法从索引0开始并在1处停止,即[0,2]:

NewList = [element[:2] for element in List]

When the start index is not specified, it is taken as None which is the same as start index of the list when None appears before the first :.

如果未指定起始索引,则将其视为“无”,这与在第一个之前出现“无”时列表的起始索引相同。

Same as:

与...一样:

NewList = [element[slice(None, 2)] for element in List] # More verbose

In Python 3, you could use extended unpacking to achieve the same thing applying the 'splat' operator *:

在Python 3中,您可以使用扩展解包来实现应用'splat'运算符*的相同功能:

NewList = [elements for *elements, _, _ in List]

#2


1  

How about this:

这个怎么样:

 for s in List:
    del s[3]
    del s[2]

That deletes in place.

那删除到位。

#3


1  

This solution uses negative indexing to allow for arbitrary length sublists, along as the original condition of two trailing digits is maintained.

此解决方案使用负索引来允许任意长度子列表,同时保持两个尾随数字的原始条件。

List =  [
        ['101', 'Dashboard', '1', '1'],
        ['102', 'Potential Cstomer', '1', '1'],
        ['102-01', 'Potential Cstomer', '1', '1'],
        ['102-02', 'Potential Cstomer Activity', '1', '1']
    ]
new_final_list = [i[:-2] for i in List]
for i in new_final_list:
   print(i)

Output:

输出:

['101', 'Dashboard'], 
['102', 'Potential Cstomer']
['102-01', 'Potential Cstomer']
['102-02', 'Potential Cstomer Activity']

#1


5  

Slice each nested list in a list comprehension. The slice notation starts at index 0 and stops at 1 i.e. [0, 2):

在列表推导中切片每个嵌套列表。切片表示法从索引0开始并在1处停止,即[0,2]:

NewList = [element[:2] for element in List]

When the start index is not specified, it is taken as None which is the same as start index of the list when None appears before the first :.

如果未指定起始索引,则将其视为“无”,这与在第一个之前出现“无”时列表的起始索引相同。

Same as:

与...一样:

NewList = [element[slice(None, 2)] for element in List] # More verbose

In Python 3, you could use extended unpacking to achieve the same thing applying the 'splat' operator *:

在Python 3中,您可以使用扩展解包来实现应用'splat'运算符*的相同功能:

NewList = [elements for *elements, _, _ in List]

#2


1  

How about this:

这个怎么样:

 for s in List:
    del s[3]
    del s[2]

That deletes in place.

那删除到位。

#3


1  

This solution uses negative indexing to allow for arbitrary length sublists, along as the original condition of two trailing digits is maintained.

此解决方案使用负索引来允许任意长度子列表,同时保持两个尾随数字的原始条件。

List =  [
        ['101', 'Dashboard', '1', '1'],
        ['102', 'Potential Cstomer', '1', '1'],
        ['102-01', 'Potential Cstomer', '1', '1'],
        ['102-02', 'Potential Cstomer Activity', '1', '1']
    ]
new_final_list = [i[:-2] for i in List]
for i in new_final_list:
   print(i)

Output:

输出:

['101', 'Dashboard'], 
['102', 'Potential Cstomer']
['102-01', 'Potential Cstomer']
['102-02', 'Potential Cstomer Activity']