tags:

views:

129

answers:

4

hey guys, my last problem ^^ say i have a string which contains html, like

html = '<td class="p11_666699"><strong>100</strong></td>'

and a list like

numbers = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 
           113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
           126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
           139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150]

basically i want to do something like this:

html = '<td class="p11_666699"><strong>' + numbers + '</strong></td>'

edit: for some reason the code tags add curly brackets when i post this, so it messes up, sorry.

+1  A: 

Are you looking for a way to join a list of numbers into a string? This will work:

' '.join(map(str, [10, 20, 30]))

Resulting in:

'10 20 30'

The second argument of map is a list, so you can place your 'numbers' list there.

This is in no way HTML specific, of course.

Eli Bendersky
+3  A: 

I think you either want this:

numbers = [100, 101, 102, 103]
output = "<td>" + ", ".join(map(str, numbers)) + "</td>"

or

output = ""
for number in numbers:
    output += "<td>" + str(number) + "</td>"
Georg
wrong: str(numbers) right: str(number)
Kai
+5  A: 

As pointed out in the comments, it's really hard to understand your question... but if you want to generate a bunch of table cells, each containing one of the numbers, use something like this:

html = ''.join('<td>%d</td>' % n for n in numbers)

Of course you can add in a class or other attribute to be applied to the table cells if you want.

David Zaslavsky
+1  A: 
html = ['<td class="p11_666699"><strong>%d</strong></td>' % number for number in numbers]

And I see David just suggested this (but joined).

jcoon