开发者

Counting removed items in a Set in Python

开发者 https://www.devze.com 2022-12-24 08:51 出处:网络
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]

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

精彩评论

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