【问题标题】:Explain python cycle with lambdas, please [closed]请用 lambdas 解释 python 循环[关闭]
【发布时间】:2019-08-13 18:30:38
【问题描述】:

我需要根据自己的需要编辑代码,但不幸的是我没有选择从零开始重写,所以我必须了解这是什么,因为截止日期是 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:我需要添加一条规则:“如果土地大小 > 9000 则......”其中土地大小在另一列中”

【问题讨论】:

  • 这就是可怕的代码。没有上下文,我真的不认为我们可以在这里提供太多帮助,抱歉,即使有了它,这也是不必要的复杂。你有我的同情:/

标签: python lambda xlsx cycle


【解决方案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]
))

好吧,它看起来仍然很疯狂,但幸运的是我们可以进一步简化:

  • in 和 not in 将给出 True 或 False 但因此我们可以做 any(...) 而不是检查 True in ...,
  • 内部列表理解[...] 独立于外部映射,因此我们可以将其重构出来,
  • 由于x in text 和下面的w 条件是and'ed,我们可以将前面的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] 中的至少一个。为什么会这样由你来判断。

【讨论】:

  • +1 这几乎就是我分析它的方式。我还拆分了两个理解并将any(not ...) 切换为all(...):x_result = any(x in text for x in _classes[_classB][1]); w_result = all(w in text for w in _classes[_classA][0]); result = x_result and w_result
  • 哎呀,any(not ...) 和 all(...) 不一样
【解决方案2】:
 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() 函数将根据某些转换函数和输入返回对象映射。 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])):

此语句将返回一个布尔值列表。

True in list(map(mylambda, _classes[_classB][1])):

如果对于_classes[_classB][1] 中的任何值,该值存在于text 中并且_classes[_classA][0] 中的某些值在text 中不存在,则这将返回True。

现在已经结束了,请烧掉这段代码,不要再说了。

【讨论】:

    【解决方案3】:

    下面一行:

    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
    

    由于我不知道你的上下文,我不可能更好地命名变量,但我希望这更易于管理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-24
      • 2020-05-27
      • 2019-12-30
      • 2016-08-20
      • 2020-08-16
      • 1970-01-01
      • 2011-04-04
      相关资源
      最近更新 更多