0

I am getting my feet wet with Python. I never done any programming or what so ever before and I really would appreciate it if someone would explain his_hers answer and not just post it since I want to learn something! Even better would be not posting the answere, but just giving hints what I should look at or something :)

I have several lists with a lot of values (numbers) one the one side. On the other side, I have a URL which needs to be updated by the numbers out of the several lists and then be saved into another list for further process.

#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 / 100 #longitudal steps the model shall perfom


#latitude
latstep = latmax - longmin
latstepx = latstep / 100 #latitudal steps the model shall perform


#create list of steps through coordinates longitude
llong = []
while longmin < longmax:
    longmin+=longstepx
    llong.append(+longmin)


#create list of steps through coordinates latitude
llat = []
while latmin < latmax:
    latmin+=latstepx
    llat.append(+latmin)


#create the URLs and store in list
for i in (llong):
    "https://api.flickr.com/services/rest/?method=flickr.photos.search&format=json&api_key=5....lback=1&page=X&per_page=500&bbox=i&accuracy=1&has_geo=1&extras=geo,tags,views,description",sep="")"

As you can see, I try to make a request to the REST API from flickr. What I dont understand is:

  1. How do I get the loop to go through my lists, insert the values from the list to a certain point in the URL?
  2. How to I tell the loop to save each URL separately after it inserted the first number out of list "llong" and "llat" and then proceed with the two next numbers.

Any hints?

Engineer2021
  • 3,288
  • 6
  • 29
  • 51
four-eyes
  • 10,740
  • 29
  • 111
  • 220

2 Answers2

0

You can use string formatting to insert whatever you want into your url:

my_list=["foo","bar","foobar"]

for word in my_list:
    print ("www.google.com/{}".format(word))
www.google.com/foo
www.google.com/bar
www.google.com/foobar

The {} is used in your string wherever you want to insert.

To save them to a list you can use zip, insert using string formatting and then append to a new list.

urls=[]
for lat,lon in  zip(llat,llong):
    urls.append("www.google.com/{}{}".format(lat,lon))

Python string formatting: % vs. .format

I think the .format() method is the preferred method as opposed to using the "www.google.com/%s" % lat syntax. There are answers here that discuss some of the differences.

The zip function is best explained with an example:

Say we have 2 lists l1 and l2:

l1 = [1,2,3]
l2 = [4,5,6]

If we use zip(l1,l2)the result will be:

[(1, 4), (2, 5), (3, 6)]

Then when we loop over the two zipped lists like below:

for ele_1,ele_2 in zip(l1,l2):
    first iteration ele_1 = 1, ele_2 = 4
    second iteration ele_1 = 2 ele_2 = 5 and so on ...
Community
  • 1
  • 1
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • You already beat me to the answer. Can you also add `print "www.google.com/%s" % word`? And do the same on `urls.append("www.google.com/%f%f" % (lat, lon))`? This will make your answer more comprehensive thus giving OP more knowledge and options to use. – alvits Jul 05 '14 at 19:55
  • 1
    @alvits I think string formatting is the preferred method, I will add a link to an SO question that discusses the difference. – Padraic Cunningham Jul 05 '14 at 20:03
  • I have a question. I try to reconstruct your code and run into a couple of problems. Copy and pasting the first part of your code gives me the same error message - `Invalid Snytax`. It is highlighting the last `"` of `"www.google.com/{}"` – four-eyes Jul 06 '14 at 00:41
  • @Cristoph are you using python3? I added parens to the print statement , that may be the issue – Padraic Cunningham Jul 06 '14 at 00:48
  • yeah, python 3.3. i replaced them already with `'` but that did not help. Or get rid of them? – four-eyes Jul 06 '14 at 00:51
  • doing it with `()` it seems like it does not recognize it as a string. it says `www.` is not defined. – four-eyes Jul 06 '14 at 00:52
  • @Cristoph don't paste the `www.google.com/foo` etc.. that is just the output I was showing you – Padraic Cunningham Jul 06 '14 at 00:54
  • Ai, of course. Oh man, so much to learn. I am rather a social scientist person and working with scripting and another language is rather hard for me. One last question. I understand everything perfectly (I hope) but what does not make sense to me is the last loop. What are we doing this for? – four-eyes Jul 06 '14 at 00:56
  • the `for ele_1,ele_2 in zip(l1,l2)`? – Padraic Cunningham Jul 06 '14 at 00:57
  • Yes. What is it doing exactly? – four-eyes Jul 06 '14 at 00:59
  • I am just using pseudocode in the loop to show you how zip works, how it puts the elements at the corresponding elements of each list in a single tuple and how to get the two values from the tuple at once in the loop. The code you want for your own project is in the middle. – Padraic Cunningham Jul 06 '14 at 01:04
  • Could I insert even four values? I am working on that right now. And even a fifth value, which does not has the length of the lists, but changes as well (between 1 and 8) and needs to be looped – four-eyes Jul 07 '14 at 00:02
  • Thats exactly what I tried but when I try to run and print it, the shell stays empty. Nothing is coming up... – four-eyes Jul 07 '14 at 00:15
  • it definitely works, maybe your code gets stuck in a while loop. Put in print statements to see what is happening – Padraic Cunningham Jul 07 '14 at 00:20
  • alright, it seems like it does not get stuck at a while.loop. I try a little bit around – four-eyes Jul 07 '14 at 00:34
0
myUrls=[]
for i in range len(llat) # len(llong) is valid if both have same size.
    myUrls.append(newUrl(llat[i]),llong[i]) 


def newUrl(lat,long):
    return "www.flickr.......lat="+lat+".....long="+long+"...."
  • 1
    Please explain **why** this code works to discourage copying and pasting of code. The OP should understand the ramification behind your choices to allow him/her to ultimately decide whether or not this piece of code is ultimately the one that he/she wants to use. – rayryeng Jul 05 '14 at 20:54