python - Mako template sum -


i have problem creating mako template, take list of numbers , output sum. example:

list = [1, 2, 3, 4, 5] 

output:

1 + 2 + 3 + 4 + 5 

i want list passed argument template. way go around using python ' + '.join(list)? know can use \ escape new line characters , in loop special care needs taken regard last +, quite ugly.

thanks!

built in function sum() should work fine, see python doc sum

your template body should contain just

def template(context, numbers_list): return sum(numbers_list)

or, if numbers passed in string:

def template(context, numbers_list): return sum(int(x) x in numbers_list)


Comments

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -