[英]Using map and filter function on a sublist in python
L = [[5, 0, 6], [7, 22], [0, 4, 2], [9, 0, 45, 17]]
I do have this list and my task is to remove the 0
's. 我确实有此列表,我的任务是删除
0
。 I have to use both map()
and filter()
function. 我必须同时使用
map()
和filter()
函数。
The hint is that this task is solvable with a single expression using map, filter and lambda expressions. 提示该任务可以通过使用map,filter和lambda表达式的单个表达式解决。
I have been trying to figure this out for quite a while but I just don't get it. 我已经尝试了好一阵子了,但我还是不明白。 You don't have to solve it completely, but help would be appreciated.
您不必完全解决它,但将不胜感激。
I assume I use map()
to iterate over the outer lists, but how do I call filter()
with the sublist? 我假设我使用
map()
遍历外部列表,但是如何使用子列表调用filter()
?
L2 = map(lambda x: filter(lambda x: x<>0 ,L),L)
Using map
and filter
you could write the following 使用
map
和filter
您可以编写以下内容
>>> list(map(lambda i: list(filter(lambda i: i != 0, i)), L))
[[5, 6], [7, 22], [4, 2], [9, 45, 17]]
For what it's worth, I'd prefer a nested list comprehension 对于它的价值,我更喜欢嵌套列表理解
>>> [[i for i in j if i != 0] for j in L]
[[5, 6], [7, 22], [4, 2], [9, 45, 17]]
You can use the fact that if the first argument to filter()
is None
then only those items that are "true" will be retained. 您可以使用以下事实:如果
filter()
的第一个参数为None
则仅保留那些为“ true”的项。 0
is considered false, so 0 will be removed. 0
被认为是错误的,因此0将被删除。
A solution can be written like this: 解决方案可以这样写:
>>> L = [[5, 0, 6], [7, 22], [0, 4, 2], [9, 0, 45, 17]]
>>> map(lambda l: filter(None, l), L)
[[5, 6], [7, 22], [4, 2], [9, 45, 17]]
If you are using Python 3 then you can call list()
on the result of the filter()
and the map()
to get the required list: 如果您使用的是Python 3,则可以在
filter()
和map()
的结果上调用list()
map()
以获得所需的列表:
>>> map(lambda l: filter(None, l), L)
<map object at 0x7fb34856be80>
>>> list(map(lambda l: list(filter(None, l)), L))
[[5, 6], [7, 22], [4, 2], [9, 45, 17]]
Now that's getting less readable. 现在,这变得越来越难以理解。 A list comprehension is probably better.
列表理解可能更好。
>>> [[item for item in l if item] for l in L]
[[5, 6], [7, 22], [4, 2], [9, 45, 17]]
The key to me is to think about types, which is a somewhat foreign concept in Python. 对我来说,关键是要考虑类型,这是Python中有点陌生的概念。
map
takes a function and an iterable, and returns an iterable that is the result of applying that function to each element of the iterable argument. map
接受一个函数和一个可迭代的对象,并返回一个可迭代对象,该对象是将该函数应用于可迭代参数的每个元素的结果。
map(f: "some function",
lst: "some iterable") -> "(f(e) for e in lst)"
in this case, the elements of your outer lists are themselves lists! 在这种情况下,外部列表的元素本身就是列表! And you're planning to apply the
filter
function to each to remove the zeroes. 并且您打算将
filter
函数应用于每个函数以消除零。 filter
expects a function (that itself expects an element, then returns True
if that element should be in the result, or False
if it should be removed) and a list of those elements. filter
需要一个函数(它本身需要一个元素,如果该元素应该在结果中,则返回True
如果应该将其删除,则返回False
)以及这些元素的列表。
filter(f: "some_func(e: 'type A') -> Bool",
lst: "some_iterable containing elements of type A") ->
"(e for e in lst if f(e))":
Your filter
function then is just filter(lambda x: x != 0, some_list)
, or lambda sublst: filter(lambda x: x!=0, sublst)
. 然后,您的
filter
函数就是filter(lambda x: x != 0, some_list)
或lambda sublst: filter(lambda x: x!=0, sublst)
。 Apply that in the map
and you'll get: 将其应用到
map
,您将获得:
map(lambda sublst: filter(lambda x: x!=0, sublst), lst)
or 要么
[[x for x in xs if x!=0] xs in xss]
This is much easier to express in Haskell, which deals with map
and filter
much more naturally. 这在Haskell中更容易表达,Haskell更自然地处理
map
和filter
。
map (filter (/=0)) lst
-- or still with a list comprehension
[[x | x <- xs, x!=0] | xs <- xss]
您可以在过滤器函数中使用None参数,并执行类似的操作
[list(filter(None, i)) for i in a]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.