Python:Frozensets比较 [英] Python: frozensets comparison
本文介绍了Python:Frozensets比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下脚本:
# 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< = singleFrozenset,b)len(i)== 1.但是,我无法找出第一个条件,即与之匹配的案例是1、4、6和7.在这些案例中,我找不到这些冻结集之间的链接! 知道为什么吗?
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?
推荐答案
设置运算符<=
等效于.issubset()
方法. A <= B
为真,且仅当A的每个元素也属于B时.
Set operator <=
is equivalent to the .issubset()
method. A <= B
is true if and only if each element of A also belongs to B.
这篇关于Python:Frozensets比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文