2016-12-30 7 views
-2

オブジェクトのブール値プロパティ(配列内のオブジェクト)がTrueに設定されている場合にのみ、いくつかのものを出力する必要があります。私は現在しようとしている:私はここで何かが欠けていリスト内包のインラインプリント

print("Points of Interest: " + (" ".join([str(poi.name) for poi in currentRoom.pointsOfInterest] if [poi.found for poi in currentRoom.pointsOfInterest] else 0))) 

str(poi.name)がfalseに設定されているオブジェクトのブール値プロパティ(poi.found)にもかかわらず、印刷されますよう。

アドバイスはありますか?事前に

おかげ

答えて

2

[poi.found for poi in currentRoom.pointsOfInterest]は、リストを作成します。それにオブジェクトがあれば、真実になります。これらのオブジェクトは虚偽である場合もあります - リスト全体が空でない限り、全体のリストは真実として評価されます。あなたが見たい正確な動作に応じて、anyまたはallを使用する必要があります:

>>> if [0, 0]: print('y') 
... 
y 
>>> if any([0,0]): print('y') 
... 
>>> if all([0,0]): print('y') 
... 
>>> if any([]): print('y') 
... 
>>> if all([]): print('y') 
... 
y 
>>> if any([0,1]): print('y') 
... 
y 
>>> if all([0,1]): print('y') 
... 
>>> if any([1,1]): print('y') 
... 
y 
>>> if all([1,1]): print('y') 
... 
y 
+0

は、それを解決したこと、ありがとうございます! – Wretch11