[英]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.