[Numpy-discussion] a 'for' loop within another 'for' loop?

Sebastien.deMentendeHorne at electrabel.com Sebastien.deMentendeHorne at electrabel.com
Wed Jul 14 08:41:09 CDT 2004


I could not resist to propose an other solution:
 
r = array([0,2,5,6,8])
l = (r[:,NewAxis] + r[NewAxis,:]).flat
 
 
 -----Original Message-----
From: Hee-Seng Kye [mailto:kyeser at earthlink.net]
Sent: mercredi 14 juillet 2004 4:22
To: numpy-discussion at lists.sourceforge.net
Subject: [Numpy-discussion] a 'for' loop within another 'for' loop?



Hi. I wrote a program to calculate sums of every possible combinations of
two indices of a list. The main body of the program looks something like
this: 


r = [0,2,5,6,8] 

l = [] 


for x in range(0, len(r)): 

for y in range(0, len(r)): 

k = r[x]+r[y] 

l.append(k) 

print l 


1. I've heard that it's not a good idea to have a 'for' loop within another
'for' loop, and I was wondering if there is a more efficient way to do this.



2. Does anyone know if there is a built-in function or module that would do
the above task in NumPy or Numarray (or even in Python)? 


I would really appreciate it if anyone could let me know. 


Thanks for your help!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20040714/8883cf76/attachment-0001.html 


More information about the Numpy-discussion mailing list