简体   繁体   English

从嵌套列表中的所有子列表中删除第一个元素

[英]Removing the first element from all the sub-list in a nested list

I have a list of lists such that我有一个列表列表,这样

a = [["append",5],["insert",7],["print",9,10]]

How can I remove the first value from each sublist within the list using map and lambda function?如何使用map和 lambda 函数从列表中的每个子列表中删除第一个值?

This is my code这是我的代码

m =  map(lambda x : x.remove(x[0]), a)

And my output for list(m) is [None, None, None]我的list(m)输出是[None, None, None]

Can anybody please help me understand why this is happening and how can the code be corrected?谁能帮我理解为什么会发生这种情况以及如何更正代码? Thanks!!!谢谢!!!

If you really want to do the map way, here is the working version:如果你真的想做地图的方式,这里是工作版本:

m = list(map(lambda x: x[1:], a)) 

# or even better just use List Comp.

m1 = [x[1:] for x in a]

assert m == m1  

>>> m
[[5], [7], [9, 10]]

Do you want to modify the existing lists or create new ones?您要修改现有列表还是创建新列表? Daniel Hao already covered creating new ones , so I'll cover modifying the existing ones. Daniel Hao 已经介绍了如何创建新的,所以我将介绍如何修改现有的。

You're getting None s because in Python, mutator methods return None , and list.remove() is one such mutator method.你得到None是因为在 Python 中,mutator 方法返回None ,而list.remove()就是这样一种 mutator 方法。 For more info, see Why does append() always return None in Python?有关更多信息,请参阅为什么 append() 在 Python 中总是返回 None?

The better solution is to not use map at all, because you'd be using it primarily for side-effects, which is bad, the same way that using a list comprehension for side-effects is bad .更好的解决方案是根本不使用map ,因为您主要将它用于副作用,这很糟糕,就像使用列表理解来处理副作用一样 bad Just use a plain for-loop:只需使用一个普通的 for 循环:

for sublist in a:
    del sublist[0]

Also note that I'm using del here.另请注意,我在这里使用del Using remove() as you are is redundant: instead of simply saying, "delete the first element", you're saying "get the first element then find the first occurrence and remove it".使用remove()是多余的:不是简单地说“删除第一个元素”,而是说“获取第一个元素,然后找到第一个出现并删除它”。

Here a functional approach to slice the items of each (sub)list.这是一种对每个(子)列表的项目进行切片的功能方法。

Sometimes it is more the effort than the undertaking , see wjandrea 's answer.有时,努力比事业更重要,请参阅wjandrea的回答。

from operator import itemgetter

a = [["append",5],["insert",7],["print",9,10]]


print(list(map(itemgetter(slice(1, None)), a)))

Here a dirty hack to fix the behavior of the approach in the question:这是一个肮脏的黑客来解决问题中方法的行为:

print(list(map(lambda x: (x.pop(0), x)[1], a)))

Also remove can be used instead pop .也可以使用remove代替pop

Remember that also the original object will be affected by such operations!请记住,原始对象也会受到此类操作的影响!

It is happening because remove function is not returning anything whereas pop returns whatever element you remove from the list and instead of using "m" which is returned values of function remove use "a" which is modified list you can also use pop for it like this.发生这种情况是因为 remove 函数没有返回任何内容,而 pop 返回您从列表中删除的任何元素,而不是使用“m”,它是函数 remove 的返回值,使用“a”,它是修改后的列表,您也可以使用 pop 作为它这个。

a = [["append",5],["insert",7],["print",9,10]]
m = list(map(lambda x : x.pop(0), a))

Than if you print m you will get如果你打印 m 你会得到

['append', 'insert', 'print']

And if you print a than you will get如果你打印一个比你会得到

[[5], [7], [9, 10]]

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

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