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!

l = Numeric.add.outer(r, r).flat oughta do the trick. Should work for numarray, too. On Tue, 13 Jul 2004, 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!

Thank you so much. It works beautifully! On Jul 14, 2004, at 1:01 AM, Warren Focke wrote:
l = Numeric.add.outer(r, r).flat oughta do the trick. Should work for numarray, too.
On Tue, 13 Jul 2004, 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!
------------------------------------------------------- This SF.Net email sponsored by Black Hat Briefings & Training. Attend Black Hat Briefings & Training, Las Vegas July 24-29 - digital self defense, top technical experts, no vendor pitches, unmatched networking opportunities. Visit www.blackhat.com _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion

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!
participants (3)
-
Hee-Seng Kye
-
Paul F. Dubois
-
Warren Focke