[英]understanding a python snippet about a boolean variable
我是python的初学者,我不知道下面的这一行用于什么? 这是布尔值吗?(用于&如果在句子中?)如果有人知道,请解释。 谢谢
taken_match = [couple for couple in tentative_engagements if woman in couple]
###
for woman in preferred_rankings_men[man]:
#Boolean for whether woman is taken or not
taken_match = [couple for couple in tentative_engagements if woman in couple]
if (len(taken_match) == 0):
#tentatively engage the man and woman
tentative_engagements.append([man, woman])
free_men.remove(man)
print('%s is no longer a free man and is now tentatively engaged to %s'%(man, woman))
break
elif (len(taken_match) > 0):
...
Python具有一些漂亮的语法,可以快速创建列表。 您在这里看到的是列表理解-
taken_match = [couple for couple in tentative_engagements if woman in couple]
take_match将会是女人所在的所有夫妇的列表 -基本上,这会过滤掉 女人 不在这对夫妇中的所有夫妇。
如果我们在没有列表理解的情况下写出来:
taken_match = []
for couple in couples:
if woman in couple:
taken_match.append(couple)
如您所见..列表理解更酷:)
在该行之后,您要检查taked_match的长度是否为0,如果为0,则找不到其中有那个女人的情侣,因此我们在男人和女人之间进行了订婚,然后继续前进。 如果还有其他您不理解的内容,请随时提出!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.