我想做的是:
pair1 = (1,2)
pair2 = (3,3)
pair3 = (3,2)
# Is there a way that I can compare any of these two objects and yields the following:
def myComp(...):
#...
myComp(pair1,pair2) gives False
myComp(pair1,pair3) gives True #They both have 2 at index 1
myComp(pair1,pair3) gives True #They both have 3 at index 0
任何想法或建议将不胜感激。在
有一些内置函数可以比硬编码if语句条件容易得多。您可以使用
zip
和any
:^{pr2}$
结果是,使用}。如果是,则结果是
zip
将这两个列表压缩在一起,这将创建一个元组生成器。这是在生成器理解中解包的。any
然后测试是否有任何比较x == y
是{True
,并返回。否则,返回False
。在这种方法适用于任意大小的列表,只要它们相等。在
您的mycmp函数只需要一个if-to-do比较,因此它将如下所示:
相关问题 更多 >
编程相关推荐