【问题标题】:How to create a new list from list1 - list 2 in python [duplicate]如何在python中从list1-list 2创建一个新列表[重复]
【发布时间】:2016-10-27 07:31:35
【问题描述】:

我有 list1= [com.x1 ,com.x2 com.x3] 并且我还有一个列表 list2= [com.x3]。

我还需要一个列表,如 list3=[com.x1 , com.x2 ],例如 list1-list2。

请帮帮我。

【问题讨论】:

  • 你听说过set吗?
  • 您可以使用列表推导式或生成器表达式(如果您正在处理大列表)[x for x in list1 if x not in list2] (x for x in list1 if x not in list2)

标签: python python-2.7 python-3.x


【解决方案1】:

列表推导可以:

[x for x in list1 if x not in list2]

【讨论】:

    猜你喜欢
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 2015-02-01
    • 2020-12-19
    • 1970-01-01
    • 2019-09-20
    • 2021-09-18
    相关资源
    最近更新 更多