Subtracting two lists in Python

前端 未结 13 1571
一整个雨季
一整个雨季 2020-12-02 15:31

In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I\'d like to do something like

13条回答
  •  自闭症患者
    2020-12-02 16:08

    I know "for" is not what you want, but it's simple and clear:

    for x in b:
      a.remove(x)
    

    Or if members of b might not be in a then use:

    for x in b:
      if x in a:
        a.remove(x)
    

提交回复
热议问题