Subtracting two lists in Python

前端 未结 13 1611
一整个雨季
一整个雨季 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

    You can use the map construct to do this. It looks quite ok, but beware that the map line itself will return a list of Nones.

    a = [1, 2, 3]
    b = [2, 3]
    
    map(lambda x:a.remove(x), b)
    a
    

提交回复
热议问题