python - Adding more than one list -


i want try many ways function of python so, want not use zip use other python function ,how can to?

this use zip , adding more 1 list: want other way not use zip:

x = [12, 22, 32, 42, 52, 62, 72, 82, 29] y = [10, 11, 12, 13, 14, 15, 16, 17, 18]  def add_list(i, j):     l = []     n, m in zip(x, y):         l.append(n + m)     return l 

i know way,

without using zip, can use map:

from operator import add  x = [12, 22, 32, 42, 52, 62, 72, 82, 29] y = [10, 11, 12, 13, 14, 15, 16, 17, 18] res = map(add, x, y) # [22, 33, 44, 55, 66, 77, 88, 99, 47] 

note if iterables of different lengths, shortest padded none cause typeerror in add instead of zip truncate shortest list.

on aside there's absolutely nothing wrong using zip - i'd re-write list-comp though, eg:

[sum(items) items in zip(x, y)] 

this scales doing zip(x, y, z, another_list) etc...


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -