我需要知道字母b(最好是大寫還是小寫)都包含在列表中。在python列表中搜索子字符串
我的代碼:
List1=['apple', 'banana', 'Baboon', 'Charlie']
if 'b' in List1 or 'B' in List1:
count_low = List1.count('b')
count_high = List1.count('B')
count_total = count_low + count_high
print "The letter b appears:", count_total, "times."
else:
print "it did not work"
爲了解釋大寫/小寫,我會改變mycount語句來使用'item.lower()。count('b')' – sal
@sal好主意。 – TerryA
謝謝。有沒有辦法在列表中搜索而不搜索每個字符串? – Robo