简体   繁体   English

Python:Frozensets比较

[英]Python: frozensets comparison

consider the following script: 考虑以下脚本:

# 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"

and the mysterious output is 而神秘的输出是

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

These if-else conditions are checking for two cases a) i <= singleFrozenset, and b) len(i) == 1. The second condition is simple; 这些if-else条件正在检查以下两种情况:a)i <= singleFrozenset,b)len(i)== 1。 however, I couldn't figure out the first condition where the cases that matched with are 1, 4, 6, and 7. I couldn't find a link between these frozen sets in these cases! 但是,我无法找出第一个条件,即与之匹配的案例是1、4、6和7。在这些案例中,我找不到这些冻结集之间的链接! Any idea why? 知道为什么吗?

Set operator <= is equivalent to the .issubset() method. 设置运算符<=等效于.issubset()方法。 A <= B is true if and only if each element of A also belongs to B. 当且仅当A的每个元素也属于A <= B为真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM