2012-09-05 26 views
1

我在學習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" 
    ] 
+2

如果你只是想串個城市使用添加索引正常for循環或列表理解,其中我是你的索引。但你的問題仍然是一個謎,你添加的代碼並不能真正幫助澄清事情。 – LarsVegas

回答

2

你的網址,例如不要讓有很大的意義,問題本身(當然,我編輯的最糟糕的客場)難以閱讀和理解。如果可能的話,嘗試編輯它並使其更清晰。

我不知道,但它聽起來像是你想使用list comprehensions

url_array = [ "http://%s/stuff" % x for x in cities ] 

會產生:

url_array = [ "http://hamburg/stuff", "http://berlin/stuff" ... ] 
+0

thx每一個! – thegrunt

+0

thanx ** 1E4 XD! – thegrunt

相關問題