Python: frozensets comparison

Deadly 提交于 2019-12-20 04:25:10

问题


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; 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! Any idea why?


回答1:


Set operator <= is equivalent to the .issubset() method. A <= B is true if and only if each element of A also belongs to B.



来源:https://stackoverflow.com/questions/41476711/python-frozensets-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!