我在學習Python,並在scrapy中寫了一個小刮板。目前我正與URL的一個硬編碼的數組列表的工作,像這樣:如何通過索引將數組值發送到具有循環的另一個數組中?
"http city1 /%(searchterm)s"dict(searchterm=searchterm),
"http city2 /%(searchterm)s"dict(searchterm=searchterm),
"http city3 /%(searchterm)s"dict(searchterm=searchterm),
我認爲這是可以通過調用索引來減少我的代碼以解析城市(城市)的數組到這個url_array
來自城市陣列的那些城市,這些城市在目標陣列內用循環聲明聲明這些城市。
這在技術上有可能嗎?我的下一步應該是什麼?我該怎麼Google?
cities = ["hamburg", "berlin", "muenchen"]
citiesCount = len(cities)
url_array = [
http:"%(cities[1])s"
while citiesCount in range(1, len(cities):
http:"%(cities[0+1]s"
]
如果你只是想串個城市使用添加索引正常for循環或列表理解,其中我是你的索引。但你的問題仍然是一個謎,你添加的代碼並不能真正幫助澄清事情。 – LarsVegas