[英]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))
, filter
和lambda
做什么,因为它们与模式不匹配,所以它们被放置在新列表中?
There are two parts to your question. 您的问题分为两部分。
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. 您可以在函数主体中使用多行和语句。
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.