[英]python code with lambda and filter
Can anyone help me understand the following piece of python code: 谁能帮助我理解以下python代码段:
for i, char in filter(lambda x: x[1] in str1, enumerate(str2)):
# do something here ...
str1 and str2 are strings, I sort of understand that the "lambda x: x[1] in str1" is filtering condition, but why x[1] ? str1和str2是字符串,我有点理解“ lambda x:str1中的x [1]”是过滤条件,但是为什么x [1]是呢?
How can I convert this for loop into a lower level (but easier to understand) python code ? 我如何才能将此for循环转换为较低级别(但更易于理解)的python代码?
Thanks 谢谢
This appears functionality equivalent to: 看来功能等同于:
for i, char in enumerate(str2):
if char in str1:
# do something here
filter
is taking a list of tuples consisting of the index and elements of str2
, filtering out those elements that do not appear in str1
, then returning a iterable of the remaining indices and elements from str2
. filter
正在获取由str2
的索引和元素组成的元组列表,过滤掉那些没有出现在str1
元素,然后从str2
返回剩余索引和元素的可迭代值。
Because of enumerates
. 因为
enumerates
。
Enumerates returns tuples of (index, value)
for an iterable of values. 枚举返回
(index, value)
元组,以获取可迭代的值。
x
is a tuple of index, char. x
是索引char的元组。
I would write lambda (index, char): char in str1
for clarity 为了清楚起见
lambda (index, char): char in str1
我将lambda (index, char): char in str1
编写lambda (index, char): char in str1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.