Unable to put a variable in Python's print

My code:

year=[51-52,53,55,56,58,59,60,61]
photo=[{70,72,73},{64,65,68},{79,80,81,82},{74,77,78},{60,61,62},{84,85,87},{57,58,59},{53,54,55,56}]

for i in range(7):
    print "<img src=\"http://files.getdropbox.com/u/100000/Akuja/",year,"/P10104",photo,".JPG\">"

I run it and I get

File "/tmp/aku.py", line 2
    photo=[{70,72,73},{64,65,68},{79,80,81,82},{74,77,78},{60,61,62},{84,85,87},{57,58,59},{53,54,55,56}]
              ^
SyntaxError: invalid syntax

I want the following output

<img src="http://files.getdropbox.com/u/100000/Akuja/51-52/P1010470.JPG">
<img src="http://files.getdropbox.com/u/100000/Akuja/51-52/P1010472.JPG">
<img src="http://files.getdropbox.com/u/100000/Akuja/51-52/P1010473.JPG">

<img src="http://files.getdropbox.com/u/100000/Akuja/53/P1010464.JPG">
<img src="http://files.getdropbox.com/u/100000/Akuja/53/P1010465.JPG">
<img src="http://files.getdropbox.com/u/100000/Akuja/53/P1010468.JPG">

...

I also run the same code with the syntax for list unsuccessfully photo={[70,72,73],...}

How can you make the Python code to work?

Upvotes: 0

Views: 313

Answers (3)

stefanB
stefanB

Reputation: 79840

Since you are trying to iterate over years and print photos for that year I would do it this way:

year=["51-52","53","55","56","58","59","60","61"]
photo=[(70,72,73),(64,65,68),(79,80,81,82),(74,77,78),(60,61,62),
       (84,85,87),(57,58,59),(53,54,55,56)]

# this dictionary will be generated with the code below
#photos = {
#    "51-52": (70,72,73),
#    "53": (64,65,68),
#    "55": (79,80,81,82),
#    "56": (74,77,78),
#    "58": (60,61,62),
#    "59": (84,85,87),
#    "60": (57,58,59),
#    "61": (53,54,55,56)
#}


photos = {}                         # create photos dictionary

for y in xrange(len(year)):
    photos[year[y]] = photo[y]

years = photos.keys()               # sort the years
years.sort()

for year in years:
    for photo in photos[year]:
        print "<img src=\"http://files.getdropbox.com/u/100000/Akuja/"+year+"/P10104"+str(photo)+".JPG\">"

You get:

<img src="http://files.getdropbox.com/u/100000/Akuja/51-52/P1010470.JPG">
<img src="http://files.getdropbox.com/u/100000/Akuja/51-52/P1010472.JPG">
<img src="http://files.getdropbox.com/u/100000/Akuja/51-52/P1010473.JPG">
<img src="http://files.getdropbox.com/u/100000/Akuja/53/P1010464.JPG">
<img src="http://files.getdropbox.com/u/100000/Akuja/53/P1010465.JPG">
<img src="http://files.getdropbox.com/u/100000/Akuja/53/P1010468.JPG">

I would store photos and years in dictionary as shown above but if you have years and photos in separate lists (as in your question) you can create the photos like this, note years are in quotes "":

photos = {}

for y in xrange(len(year)):
    photos[year[y]] = photo[y]

Upvotes: 1

tdelev
tdelev

Reputation: 863

So here is your simple solution to your simple problem

year=['51-52', '53', '55' , '56' , '58', '59', '60', '61']
photo=[[70,72,73], [64,65,68],[79,80,81,82],[74,77,78],[60,61,62],[84,85,87],[57,58,59],[53,54,55,56]]

for i in range(len(year)):
   for j in range(len(photo[i])):
       print '<img src=\"http://files.getdropbox.com/u/100000/Akuja/%s/P10104%s.JPG>' % (year[i], photo[i][j])

Upvotes: 2

Mark Rushakoff
Mark Rushakoff

Reputation: 258358

Braces are used to indicate a dictionary (associative array). You want to use square brackets, which indicates a list.

Also you probably don't want 51-52 in that first line, as that will evaluate to -1. You should put "51-52" to ensure that it is a string.

Then to get the indexing that you seem to want, you need to do year**[i]** instead of just year, which prints out the whole list.

Upvotes: 3

Related Questions