[英]Loop through list and changing indexes
我在這里處理這段代碼,基本上是創建一個bbox並將該bbox拆分為許多較小的bbox,因為一個請求只能訪問4000個元數據。
#borders of the bbox
longmax = 15.418483 #longitude top right
longmin = 4.953142 #longitude top left
latmax = 54.869808 #latitude top
latmin = 47.236219 #latitude bottom
#longitude
longstep = longmax - longmin
longstepx = longstep / 10 #longitudal steps the model shall perfom
print (longstepx)
#latitude
latstep = latmax - latmin
latstepx = latstep / 10 #latitudal steps the model shall perform
print(latstepx)
#create list of steps through coordinates longitude
llong = []
while longmin < longmax:
longmin+=longstepx
llong.append(+longmin)
print (len(llong)) #make sure lists have the same lengths
#create list of steps through coordinates latitude
llat = []
while latmin < latmax:
latmin+=latstepx
llat.append(+latmin)
print (len(llat)) #make sure lists have the same lengths
#create the URLs and store in list
urls = []
for lat,long,lat1,long1 in (zip(llat, llong,llat[+1],llong[+1])):
for pages in range (1,5):
print ("https://api.flickr.com/services/rest/method=flickr.photos.search&format=json&api_key=5..b&nojsoncallback=1&page={}&per_page=500&bbox={},{},{},{}&accuracy=1&has_geo=1&extras=geo,tags,views,description".format(pages,lat,long,lat1,long1))
print (urls)
從創建列表“ urls”開始,直到最后一部分都可以正常工作。 我希望循環遍歷列表llat和llong並遍歷這些列表,僅比前兩個多一個值。
llong llat
4 5 6 7 8 9 10 11
我希望它使用(zip(llong,llat)值“ 4”和“ 8”(有效),然后使用(zip(llong [+1],llat [+1])值“ 5”和“ 9”並將其插入到我的鏈接中。此外,我希望它插入頁碼。理想情況下,循環創建了一個包含四個數字4,5,8,9的鏈接。然后我希望它創建四個鏈接,其數字范圍為1:5,保存鏈接,然后繼續下四個數字,依此類推。
但這根本不起作用...
Puh,我希望我表達得足夠清楚。 我不是在尋找現成的解決方案。 我想學習,因為我是python的新手。
謝謝。
我想你打算寫:
#create the URLs and store in list
urls = []
for lat, long, lat1, long1 in (zip(llat, llong, llat[1:], llong[1:])):
for page in range (1,5):
print ("https://api.flickr.com/services/rest/method=flickr.photos.search&format=json&api_key=5..b&nojsoncallback=1&page={}&per_page=500&bbox={},{},{},{}&accuracy=1&has_geo=1&extras=geo,tags,views,description".format(page, lat, long, lat1, long1))
注意區別:
In [1]: L = [1,2,3]
In [2]: L
Out[2]: [1, 2, 3]
In [3]: L[1]
Out[3]: 2
In [4]: L[1:]
Out[4]: [2, 3]
另外,請注意,我們可以替換為:
llong = []
while longmin < longmax:
longmin+=longstepx
llong.append(+longmin)
有了這個:
llong = range(longmin + longstepx, longmax + longstepx, longstepx)
但是我想您打算這樣做,它會在您所在的區域包括longmin,而不會包括longmax(這與原始代碼相反)。
llong = range(longmin, longmax, longstepx)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.