我在這些程序中遇到了一些問題。我正在Python中編寫一個函數,它將一個列表和一個字符串作爲輸入列表,如果列表中列表的第二個元素等於給定的字符串,則返回「名稱在這裏」。在這種情況下,名單上有名單是這個python中的這個簡單函數無法正常工作
railway = [["Milan","Zurich"],["Zurich","Bern"],["Bern","Berlin"],["Berlin","Copenaghen"]]
我的功能是:
def travel(list , stringdestination):
i = 0
for elemento in range(len(list)):
if list[i][1] == stringdestination:
print "target reached"
當我運行:
travel(railway, "Bern")
它應該顯示:「目標達成」,但它不,它不顯示任何內容,爲什麼?
什麼時候'我'得到更新?試着改變它。 – squiguy
不要像那樣迭代列表。使用列表中的項目:'。並且不要將你的列表命名爲'list'。 – geoffspear