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
Post a Comment