Python和set()操作......&和^

时间:2014-08-13 19:42:23

标签: python json

我尝试使用python 2.7集进行一些JSON文档比较。 我已经擦除了py文档来理解集合操作

在之前的代码中,我确保JSON文档具有精确的字段匹配

# convert the JSON to two set() for later operations
currSet = set(currJSON.items())
prevSet = set(prevJSON.items())

match   = currSet & prevSet 
unmatch = currSet ^ prevSet

log.info('%d, %d, Matched: %d, UnMatched: %d' % (len(currSet), len(prevSet), len(match), len(unmatch))

我总是得到len(currSet)== len(prevSet) 并期望len(currSet)== len(匹配)+ len(不匹配)

match = S1 & S2  # this is the intersection, elements in both sets
umatch = S1 ^ S2  # this is the outersection, elements in (S1 not S2) and (S2 not S1)

不应该:len(匹配)+ len(umatch)== len(S1)

点击我的大脑......

1 个答案:

答案 0 :(得分:5)

您正在将sumunion的{​​{1}}与symmetric difference

的长度进行比较
one set