【问题标题】:Can the repetition in this line be avoided?可以避免这一行的重复吗?
【发布时间】:2016-08-04 03:49:14
【问题描述】:

'=' not in access and name + '.' not in access

我希望避免在一行 Python 代码中出现 not in accesss 的多重性。为方便起见,我在重复次数较多的情况下使用了表达式求值循环,但在重复次数较多的情况下似乎很奇怪。

【问题讨论】:

  • 我认为您以某种方式回答了自己的问题:这真的值得吗?
  • 这似乎太不合Python了。
  • re.search('=|' + name + '[.]', access) is None 避免了重复,但我不认为我的比你的更好。
  • 我不认为为了不重复 3 个单词而使用一个可以说是晦涩难懂的表达方式更符合 Pythonic...
  • 我猜我有点期待像 '=', name + '.' not in access 这样美观的东西。

标签: python repeat non-repetitive


【解决方案1】:

这是另一个选择:

all(s not in access for s in ('=', name + '.'))

由您决定这是否比您的代码更简单 - 但至少它避免了编写not in access 两次

【讨论】:

    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 2013-02-19
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    相关资源
    最近更新 更多