请用 lambda 解释 python 循环
Explain python cycle with lambdas, please
我需要根据我的需要编辑代码,但不幸的是我没有选择从零开始重写,所以我必须明白这是什么,因为截止日期是在 11 小时内。帮助后辈找到工作
if text and segment:
if "Office" in segment:
if True in list(map(lambda x: x in text, _classes[_classB][0])):
return "Класс Б"
if "Warehouse" in segment:
if True in list(map(lambda x: x in text, _classes[_classB][0])) or \
True in list(map(lambda x: x in text and True in [w not in text for w in _classes[_classA][0]],
_classes[_classB][1])):
return "Class B"
return ""
return ""
你能解释一下这到底是什么吗
True in list(map(lambda x: x in text and True in [w not in text for w in _classes[_classA][0]],_classes[_classB][1])):
或类似 "unlambda" 的东西?非常感谢
UPD:我需要添加一条规则:"if landsize is >9000 then ..." where landsize in another column
True in list(map(lambda x: x in text and True in [w not in text for w in _classes[_classA][0]],_classes[_classB][1])):
这绝对是代码的噩梦,让我们分解一下。
True in list(map(...))
map()
函数将 return 基于某些转换函数和输入的对象映射。 list()
将其转换为列表。
lambda x: x in text and True in [w not in text for w in _classes[_classA][0]],_classes[_classB][1]
我们可以把lambda取出来,变成一个函数:
# globals variables to make our lives easier. Bad practice but obviously they don't
# care about that.
text = ...
_classes = ...
def mylambda(x):
"""
Checks that x exists in text and some class doesn't appear in text.
"""
classA0 = _classes[_classA][0]
classInText= w not in text for w in classA0
return x in text and True in classInText
现在,我们可以简化它:
list(map(mylambda, _classes[_classB][1])):
此语句将 return 布尔值列表。
True in list(map(mylambda, _classes[_classB][1])):
如果对于 _classes[_classB][1]
中的任何值,该值存在于 text
中,而 _classes[_classA][0]
中的某些值不存在于 text
中,这将 return 正确。
事已至此,请把这段代码烧掉,以后不要再提了。
好的,首先让我们重新格式化它以获得更好的视角:
True in list(map(
lambda x: (
x in text
and True in [
w not in text
for w in _classes[_classA][0]
],
_classes[_classB][1]
))
好吧,它看起来仍然很疯狂,但幸运的是我们可以进一步简化:
in
和 not in
将给出 True
或 False
但是我们可以 any(...)
而不是检查 True in ...
,
- 内部列表理解
[...]
独立于外部映射,所以我们可以重构它,
- 由于
x in text
和后面的w
条件是and
,我们可以把前面的w
条件拉到快捷方式,以防它是False
所以我们得到:
w_condition = any(w not in text for w in _classes[_classA][0])
result = w_condition and any(x in text for x in _classes[_classB][1])
基本上,这似乎是在检查 text
是否包含 _classes[_classA][0]
的全部内容,并且至少包含 _classes[_classB][1]
中的一个。为什么会这样由您来判断。
下面一行:
True in list(map(lambda x: x in text and True in [w not in text for w in _classes[_classA][0]],_classes[_classB][1])):
# do smth
可以改写为:
array2 = []
for x in _classes[_classB][1]:
array1 = []
for w in _classes[_classA][0]:
if w not in text:
array1.append(True)
else
array1.append(False)
if x in text and True in array1:
array2.append(True)
else:
array2.append(False)
if True in array2:
# do smth
可以变成:
condition1Met = False
for x in _classes[_classB][1]:
condition2Met = False
for w in _classes[_classA][0]:
if w not in text:
condition2Met = True
break
if x in text and condition2Met:
condition1Met = True
break
if condition1Met:
# do smth
由于我不知道你的上下文,我不可能更好地命名变量,但我希望这样更易于管理。
我需要根据我的需要编辑代码,但不幸的是我没有选择从零开始重写,所以我必须明白这是什么,因为截止日期是在 11 小时内。帮助后辈找到工作
if text and segment:
if "Office" in segment:
if True in list(map(lambda x: x in text, _classes[_classB][0])):
return "Класс Б"
if "Warehouse" in segment:
if True in list(map(lambda x: x in text, _classes[_classB][0])) or \
True in list(map(lambda x: x in text and True in [w not in text for w in _classes[_classA][0]],
_classes[_classB][1])):
return "Class B"
return ""
return ""
你能解释一下这到底是什么吗
True in list(map(lambda x: x in text and True in [w not in text for w in _classes[_classA][0]],_classes[_classB][1])):
或类似 "unlambda" 的东西?非常感谢
UPD:我需要添加一条规则:"if landsize is >9000 then ..." where landsize in another column
True in list(map(lambda x: x in text and True in [w not in text for w in _classes[_classA][0]],_classes[_classB][1])):
这绝对是代码的噩梦,让我们分解一下。
True in list(map(...))
map()
函数将 return 基于某些转换函数和输入的对象映射。 list()
将其转换为列表。
lambda x: x in text and True in [w not in text for w in _classes[_classA][0]],_classes[_classB][1]
我们可以把lambda取出来,变成一个函数:
# globals variables to make our lives easier. Bad practice but obviously they don't
# care about that.
text = ...
_classes = ...
def mylambda(x):
"""
Checks that x exists in text and some class doesn't appear in text.
"""
classA0 = _classes[_classA][0]
classInText= w not in text for w in classA0
return x in text and True in classInText
现在,我们可以简化它:
list(map(mylambda, _classes[_classB][1])):
此语句将 return 布尔值列表。
True in list(map(mylambda, _classes[_classB][1])):
如果对于 _classes[_classB][1]
中的任何值,该值存在于 text
中,而 _classes[_classA][0]
中的某些值不存在于 text
中,这将 return 正确。
事已至此,请把这段代码烧掉,以后不要再提了。
好的,首先让我们重新格式化它以获得更好的视角:
True in list(map(
lambda x: (
x in text
and True in [
w not in text
for w in _classes[_classA][0]
],
_classes[_classB][1]
))
好吧,它看起来仍然很疯狂,但幸运的是我们可以进一步简化:
in
和not in
将给出True
或False
但是我们可以any(...)
而不是检查True in ...
,- 内部列表理解
[...]
独立于外部映射,所以我们可以重构它, - 由于
x in text
和后面的w
条件是and
,我们可以把前面的w
条件拉到快捷方式,以防它是False
所以我们得到:
w_condition = any(w not in text for w in _classes[_classA][0])
result = w_condition and any(x in text for x in _classes[_classB][1])
基本上,这似乎是在检查 text
是否包含 _classes[_classA][0]
的全部内容,并且至少包含 _classes[_classB][1]
中的一个。为什么会这样由您来判断。
下面一行:
True in list(map(lambda x: x in text and True in [w not in text for w in _classes[_classA][0]],_classes[_classB][1])):
# do smth
可以改写为:
array2 = []
for x in _classes[_classB][1]:
array1 = []
for w in _classes[_classA][0]:
if w not in text:
array1.append(True)
else
array1.append(False)
if x in text and True in array1:
array2.append(True)
else:
array2.append(False)
if True in array2:
# do smth
可以变成:
condition1Met = False
for x in _classes[_classB][1]:
condition2Met = False
for w in _classes[_classA][0]:
if w not in text:
condition2Met = True
break
if x in text and condition2Met:
condition1Met = True
break
if condition1Met:
# do smth
由于我不知道你的上下文,我不可能更好地命名变量,但我希望这样更易于管理。