开发者

Counting removed items in a Set in Python

Given开发者_JS百科 two sets

a = [5,3,4,1,2,6,7]
b = [1,2,4,9]
c = set(a) - set(b)
# c -> [5,3,6,7]

is it possible to count how many items were removed from set 'a' ?


How about len(set(a)) - len(c)?

Edit: len(a) could be incorrect if a contains duplicates.


there might be a more efficient way, but

 len(set(a)-set(c))

will work


Assuming lack of duplicates: len(a)-len(c) otherwise try: len(set(a)) - len(c)


a = [5,3,4,1,2,6,7] 
b = [1,2,4,9] 
c = set(a) - set(b)

print len(c)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜