【发布时间】:2016-08-11 13:45:21
【问题描述】:
我想从我的列表中匹配正确的术语。这是我的代码:
stuff = ["cat", "dog", "house", "cat", "mouse"]
for item in stuff:
if "house" in item:
print "house good"
if "cat" in item:
print "cat good"
if "dog" in item:
print "dog good"
else:
print "nothing else"
目前的结果是这样的:
cat good
nothing else
dog good
house good
nothing else
cat good
nothing else
nothing else
但我希望结果是这样的:
cat good
dog good
house good
cat good
nothing else
由于我的 else 语句,目前脚本一直在拉“没有别的”。但是我不知道如何仅在我的列表中的一个术语与我的 if 语句中的术语不匹配时才使“没有别的”出现。有谁知道我可以做到这一点?
【问题讨论】: