简体   繁体   English

如何在一行代码中解释filter和lambda的功能?

[英]How can I explain the function of filter and lambda in a line of code?

number_plates = ["DV61 GGB",      #UK
                 "D31 EG 2A",     #F
                 "5314 10A02",    #F
                 "24TEG 5063",    #F
                 "TR09 TRE",      #UK
                 "524 WAL 75",    #F
                 "TR44 VCZ",      #UK
                 "FR52 SWD",      #UK
                 "100 GBS 12",    #F
                 "HG55 BPO"       #UK
                 ]

# Find the non-UK plates
pattern = "(?![A-Z]{2}\d{2}\s+[A-Z]{3}$)"
foreign_numbers = list(filter(lambda x: re.match(pattern, x), number_plates))

This is a part of my code. 这是我的代码的一部分。 The foreign_numbers = list(filter(lambda x: re.match(pattern, x), number_plates)) has been done by someone else for me, and I know roughly that it puts number plates into a new list if it doesn't match the pattern of UK number plate structure. foreign_numbers = list(filter(lambda x: re.match(pattern, x), number_plates))已由其他人替我完成,我大致知道,如果不匹配,它将车牌放入新列表中英国车牌结构的pattern This is a task set by my teacher therefore I also need to explain different parts of code one by one. 这是我老师设定的任务,因此我还需要一一解释代码的不同部分。

My question is: What do filter and lambda do in foreign_numbers = list(filter(lambda x: re.match(pattern, x), number_plates)) that the foreign plates are placed in the new list because they don't match the pattern? 我的问题是:在foreign_numbers = list(filter(lambda x: re.match(pattern, x), number_plates))filterlambda做什么,因为它们与模式不匹配,所以它们被放置在新列表中?

There are two parts to your question. 您的问题分为两部分。

  1. lambda is just a different way to write a function: lambda是编写函数的另一种方式:

     def find_non_uk(x): return re.match(pattern, x) 

    is the same as : 是相同的 :

     find_non_uk = lambda x: re.match(pattern, x) 

    lambda is rather limited in what you can do. lambda在您可以做的事情上非常有限。 It is essentially ilmited to one line and all has to be an expression. 它本质上只限于一行,并且都必须是一个表达式。 Using def , there are no such limitations. 使用def ,没有这样的限制。 You can use multiple lines and statements in the function body. 您可以在函数主体中使用多行和语句。

  2. filter applies the given function to each element of your list and returns only those elements of the list for which the return value is true. filter将给定函数应用于列表的每个元素,并仅返回列表中返回值为true的那些元素。 From the docstring: 从文档字符串:

    filter(function or None, iterable) --> filter object 过滤器(功能或无,可迭代)->过滤器对象

    Return an iterator yielding those items of iterable for which function(item) is true. 返回一个迭代器,该迭代器产生那些针对其function(item)为true的可迭代项。 If function is None, return the items that are true. 如果function为None,则返回true。

You could write your line like this: 您可以这样编写代码行:

foreign_numbers = list(filter(find_non_uk, number_plates))

You need the outer list() to turn the iterator into a list. 您需要外部list()将迭代器转换为列表。

If this seems too complicated and you know list comprehensions, use them: 如果这看起来太复杂,并且您知道列表推导,请使用它们:

pattern = re.compile("(?![A-Z]{2}\d{2}\s+[A-Z]{3}$)")
foreign_numbers = [x for x in number_plates if pattern.match(x)]

In list(filter(lambda x: re.match(pattern, x), number_plates)) , filter function itself returns a list of elements that passed the check (in this case matched regex pattern) -just like the filter we use to strain liquids to make sherbet. list(filter(lambda x: re.match(pattern, x), number_plates))过滤器函数本身返回通过检查的元素列表(在这种情况下为匹配的regex模式)-就像我们用来过滤的过滤器一样制作果子露的液体。 and list function converts it into list. and list函数将其转换为list。 So 所以

foreign_numbers = list(filter(lambda x: re.match(pattern, x), number_plates)) is the code that separates matched numbers into a list called foreign_numbers foreign_numbers = list(filter(lambda x: re.match(pattern, x), number_plates))是将匹配的数字分隔为一个名为foreign_numbers的列表的代码

>>>print foreign_numbers
>>>['D31 EG 2A', '5314 10A02', '24TEG 5063', '524 WAL 75', '100 GBS 12']

The function of lambda is grab one by one element from number_plates list and passes to the re.match . lambda的功能是从number_plates列表中抓取一个元素,然后传递给re.match

If you compile the pattern into a RegexObject , you don't have to create a lambda, as you can just use the object's match method: 如果将模式编译为RegexObject ,则不必创建lambda,因为您可以使用对象的match方法:

pattern = re.compile("(?![A-Z]{2}\d{2}\s+[A-Z]{3}$)")
foreign_numbers = list(filter(pattern.match, number_plates))

The match method gets called for every number plate, and if it matches, filter will keep it. 每个车号牌都会调用match方法,如果匹配, filter将保留它。

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

相关问题 有人可以解释一下这一行中的排序是如何进行的。 我看到排序是使用 lambda 函数完成的 - Can someone explain how is the sorting being done in this line. I see that the sorting is being done using lambda function 如何过滤 lambda function 中的 2 个条目,例如 a[1] + a[2] == 10? - How can I filter 2 entries in lambda function, like a[1] + a[2] == 10? 我该如何解释这行语法 - How can i explain this line of syntax 任何人都可以解释这个python代码如何逐行工作? - Can anyone please explain how this python code works line by line? 如何在 python 中准确获取 lambda 函数的代码? - How can I get exactly the code of a lambda function in python? 如何更改此代码以不使用 lambda function? - How can I change this code to not use the lambda function? 如何在数据帧中过滤`filter(lambda x:len(x [1])> = 2)`? - How can I filter `filter(lambda x:len(x[1])>=2)` in dataframe? 有人可以为我解释这个 filter() 函数吗? - can someone explain this filter() function for me? 有人可以解释这段Python代码如何工作吗? (带有lambda函数的排序列表) - Could someone explain how this piece of Python code works? (sorted list with lambda function) 解释此Lambda函数Python - Explain this Lambda function Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM