I propose 3 solutions. If someone have time to waste, he can make a benchmark to know which is the fastest and give us the results on the list.
Solution 1:
import itertools
c = [a_i-b_i for a_i, b_i in itertools.izip(a, b)]
Solution 2:
c = map(operator.sub, a, b)
#"map" will be removed from the next versions of python. So, it's not a good solution.
Solution 3:
import itertools
c = list(itertools.imap(operator.sub, a, b))
These solutions give you a list. Depending on your usage, an iterator can be better.
Cyril
On 04 Aug 2005 15:41:28 -0700, Paul Rubin <"http://phr.cx"@nospam.invalid> wrote:
"Terrance N. Phillip" <[EMAIL PROTECTED] > writes:
> Given a and b, two equal length lists of integers, I want c to be
> [a1-b1, a2-b2, ... , an-bn].
c = [a[i] - b[i] for i in xrange(len(a))]
--
http://mail.python.org/mailman/listinfo/python-list
-- http://mail.python.org/mailman/listinfo/python-list