How to remove specific element from sets inside a list using list comprehension

后端 未结 4 1761
天涯浪人
天涯浪人 2021-02-05 03:38

I think this may be related to set being mutable.

Basically, I can remove an element from a set using set.discard(element). However, set.d

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-05 04:03

    You can use set difference operator, like this

    test, empty = [{'', 'a'}, {'b', ''}], {''}
    print [x - empty for x in test]
    # [set(['a']), set(['b'])]
    

提交回复
热议问题