遍历列表并更改索引

2024-06-28 11:43:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写这段代码,基本上是创建一个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)

从创建列表“url”开始,直到最后一部分都可以正常工作。 我希望循环遍历列表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的链接。然后我要它创建4个数字在1:5范围内的链接,保存链接并继续下一个4个数字,以此类推。你知道吗

但这根本不起作用。。。你知道吗

普,我希望我表达得足够清楚。 我不是在寻找一个现成的解决方案。我想学习,因为我对python很陌生。你知道吗

谢谢。你知道吗


Tags: ofthe链接topstepsprintlatitudebbox
1条回答
网友
1楼 · 发布于 2024-06-28 11:43:30

我想你打算写:

#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)

相关问题 更多 >