【问题标题】:How to remove code duplicity in python list comprehension?如何在 python 列表理解中删除代码重复?
【发布时间】:2013-04-20 14:24:35
【问题描述】:

如何通过合并为一个列表推导来解决以下代码重复?

 myList   =[ //someList      ]      
 thierList=[ //someOtherList ]      

 if name:
          [x for x in range(2,100) if x%2 and x in mylist and x not in theirList]

 else:
          [x for x in range(2,100) if x in mylist and x not in theirList] 

【问题讨论】:

    标签: python list-comprehension code-duplication


    【解决方案1】:
    [x for x in range(2,100) if x in mylist and x not in theirList if not name or x%2 ]
    

    【讨论】:

      【解决方案2】:

      或者你可以使用集合http://docs.python.org/2/library/sets.html

      set(myList + thierList)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-19
        • 2016-05-11
        • 2021-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-20
        相关资源
        最近更新 更多