Python:frozensets比较

时间:2017-01-05 03:04:41

标签: python set comparison set-theory frozenset

考虑以下脚本:

# multipleSmallFrozensets is a list of 7 frozensets of differenet number of string objects
multipleSmallFrozensets = [
    frozenset({'YHR007C', 'YHR042W'}),
    frozenset({'YPL274W'}),
    frozenset({'YCL064C'}),
    frozenset({'YBR166C'}),
    frozenset({'YEL041W', 'YJR049C'}),
    frozenset({'YGL142C'}),
    frozenset({'YJL134W', 'YKR053C'})]

# singleFrozenset is a frozenset of 3410 string objects
singleFrozenset = frozenset({'YIL140W','YLR268W','YLR357W','YJL155C','YHR067W',
'YAL008W','YBR255W','YFR027W','YGR148C','YJR122W','YJL204C','YJL093C','YLR244C',
'YNL003C','YBR111W-A', ...})

# don't forget that i is of type frozenset [just saying!]
for i in multipleSmallFrozensets:
      if i <= singleFrozenset: print "First option entered"
      elif len(i) == 1: print "Second option entered"
      else: print "Third option entered"

并且神秘的输出是

First option entered
Second option entered
Second option entered
First option entered
Third option entered
First option entered
First option entered

这些if-else条件检查两种情况a)i&lt; = singleFrozenset,和b)len(i)== 1.第二个条件很简单;但是,我无法弄清楚匹配的情况是1,4,6和7的第一个条件。在这些情况下,我找不到这些冻结集之间的链接! 知道为什么吗?

1 个答案:

答案 0 :(得分:3)

设置运算符<=等同于.issubset()方法。当且仅当A的每个元素也属于B时才A <= B为真。