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

Paul F. Dubois paul at pfdubois.com
Wed Jul 14 05:57:07 CDT 2004


 >>> add.reduce(take(r,indices([len(r),len(r)]))).flat
array([ 0,  2,  5,  6,  8,  2,  4,  7,  8, 10,  5,  7, 10, 11, 13,  6, 
8, 11, 12, 14,  8, 10, 13, 14, 16])

Always like a good challenge in the morning. God, it is like the old 
rush of writing APL.

Hee-Seng Kye wrote:

> 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!




More information about the Numpy-discussion mailing list