[英]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.