Hello,<br>
<br>
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. <br>
<br>
Solution 1:<br>
<br>
import itertools<br>
c = [a_i-b_i for a_i, b_i in itertools.izip(a, b)]<br><br>
Solution 2:<br>
<br>
c = map(operator.sub, a, b)<br>
#"map" will be removed from the next versions of python. So, it's not a good solution.<br>
<br>
Solution 3:<br>
<br>
import itertools<br>
c = list(itertools.imap(operator.sub, a, b))<br>
<br>
<br>
These solutions give you a list. Depending on your usage, an iterator can be better.<br>
<br>
Cyril<br>
<br>
<br><div><span class="gmail_quote">On 04 Aug 2005 15:41:28 -0700, <b class="gmail_sendername">Paul Rubin</b> <"<a href="http://phr.cx"@nospam.invalid">http://phr.cx"@nospam.invalid</a>> wrote:</span>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">"Terrance N. Phillip" <<a href="mailto:mediocre_person@hotmail.com">mediocre_person@hotmail.com
</a>> writes:<br>> Given a and b, two equal length lists of integers, I want c to be<br>> [a1-b1, a2-b2, ... , an-bn].<br><br>c = [a[i] - b[i] for i in xrange(len(a))]<br>--<br><a href="http://mail.python.org/mailman/listinfo/python-list">
http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><br>