简体   繁体   English

了解有关布尔变量的python代码段

[英]understanding a python snippet about a boolean variable

I beginner in python & I don't know this line in the following is used for what? 我是python的初学者,我不知道下面的这一行用于什么? It's a Boolean value?(for & if in a sentence?) If anybody know it, please explain. 这是布尔值吗?(用于&如果在句子中?)如果有人知道,请解释。 Thanks 谢谢

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 has some pretty sweet syntax to create lists quickly. Python具有一些漂亮的语法,可以快速创建列表。 What you're seeing here is list comprehension- 您在这里看到的是列表理解-

    taken_match = [couple for couple in tentative_engagements if woman in couple]

taken_match will be a list of all the couples where the woman is in the couple- basically, this filters out all the couples where the woman is NOT in the couple. take_match将会是女人所在的所有夫妇的列表 -基本上,这会过滤 女人 不在这对夫妇中的所有夫妇。

If we were to write this out without list comprehension: 如果我们在没有列表理解的情况下写出来:

taken_match = []
for couple in couples:
     if woman in couple:
         taken_match.append(couple)

As you can see.. list comprehension is way cooler :) 如您所见..列表理解更酷:)

After that line, you're checking if the length of the taken_match is 0- if it is, no couples were found with that woman in them, so we add in an engagement between what the man and the woman, and then move on. 在该行之后,您要检查taked_match的长度是否为0,如果为0,则找不到其中有那个女人的情侣,因此我们在男人和女人之间进行了订婚,然后继续前进。 If you have any other lines you didn't understand, feel free to ask! 如果还有其他您不理解的内容,请随时提出!

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

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