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

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -