Re: Looping over lists

2007-05-06 Thread Gabriel Genellina
En Sat, 05 May 2007 05:15:32 -0300, kaens [EMAIL PROTECTED]  
escribió:

 I think the for i in range() is more readable (Maybe because I'm
 coming from a c-style syntax language background) -  but what would
 the benefits of using enumerate be (other that being more . . .
 pythonesque?)

If you want to iterate over a sequence and process each item, you could do  
this (as one would do in C):

for i in range(len(values)):
   dosomethingwith(values[i])

Problems: values[i] gets translated into values.__getitem__(i), and that  
requires a lookup for the __getitem__ method and a function call; and you  
don't actually need the index i, but it's created anyway.
In this case the best (and fastest) way in Python is:

for item in values:
   dosomethingwith(item)

No spurious variable, no method lookup, no aditional function call. But  
what if you need *also* the index? On earlier Python versions one had to  
go back to the first method; enumerate() was made just for this case:

for i, item in enumerate(values):
   dosomethingwith(item, i)

You get all the advantages of the iterator approach plus the index  
available.

-- 
Gabriel Genellina
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looping over lists

2007-05-05 Thread Paul Rubin
Tommy Grav [EMAIL PROTECTED] writes:
 In C this would be equivalent to:
 for(i = 0; i  n; i++) {
 for(j=i+1; j  n; j++) {
   print a[i], a[j]

for i in xrange(n):
   for j in xrange(i+1, n):
 print a[i], a[j]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looping over lists

2007-05-05 Thread Peter Otten
prad wrote:

 On Friday 04 May 2007 18:40:53 Tommy Grav wrote:
 Can anyone help me with the right approach for this
 in python?
 
 for each in a:
 for item in a[a.index(each)+1:]:
 print each,item
 
 will produce
 
 1 2
 1 3
 1 4
 1 5
 2 3
 2 4
 2 5
 3 4
 3 5
 4 5
 
 a.index(each) gives the index of the each value in the a list.
 then you just add 1 to it so you start at the index value beside each's
 index.

If there are equal items you may not get what you expect:
 
 items = [1, 2, 1.0]
 for a in items:
... for b in items[items.index(a)+1:]:
... print a, b
...
1 2
1 1.0
2 1.0
1.0 2
1.0 1.0

Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looping over lists

2007-05-05 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED], prad wrote:

 On Friday 04 May 2007 18:40:53 Tommy Grav wrote:
 Can anyone help me with the right approach for this
 in python?
 
 for each in a:
 for item in a[a.index(each)+1:]:
 print each,item
 
 will produce 
 
 1 2
 1 3
 1 4
 1 5
 2 3
 2 4
 2 5
 3 4
 3 5
 4 5

But only if the elements in the list are unique.  And the runtime is
suboptimal because `index()` is doing a linear search -- the outer loop
becomes slower and slower with each iteration.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looping over lists

2007-05-05 Thread kaens
I think the for i in range() is more readable (Maybe because I'm
coming from a c-style syntax language background) -  but what would
the benefits of using enumerate be (other that being more . . .
pythonesque?)

On 5/5/07, Dennis Lee Bieber [EMAIL PROTECTED] wrote:
 On Fri, 4 May 2007 19:26:17 -0700, [EMAIL PROTECTED] (Alex Martelli)
 declaimed the following in comp.lang.python:

  for i in range(n):
  for j in range(i+1, n):
  print a[i], a[j]
 
 Ah, but wouldn't the cleaner Python be something like


  a = [1, 2, 3, 4, 5, 3, 6]   #just to confuse matters
  for pos, val in enumerate(a):
 ... for v2 in a[pos+1:]:
 ... print val, v2
 ...
 1 2
 1 3
 1 4
 1 5
 1 3
 1 6
 2 3
 2 4
 2 5
 2 3
 2 6
 3 4
 3 5
 3 3
 3 6
 4 5
 4 3
 4 6
 5 3
 5 6
 3 6
 
 --
 WulfraedDennis Lee Bieber   KD6MOG
 [EMAIL PROTECTED]   [EMAIL PROTECTED]
 HTTP://wlfraed.home.netcom.com/
 (Bestiaria Support Staff:   [EMAIL PROTECTED])
 HTTP://www.bestiaria.com/
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looping over lists

2007-05-05 Thread Dustan
On May 5, 3:15 am, kaens [EMAIL PROTECTED] wrote:
 I think the for i in range() is more readable (Maybe because I'm
 coming from a c-style syntax language background) -  but what would
 the benefits of using enumerate be (other that being more . . .
 pythonesque?)

It doesn't create a whole new list just for iterating.

 On 5/5/07, Dennis Lee Bieber [EMAIL PROTECTED] wrote:

  On Fri, 4 May 2007 19:26:17 -0700, [EMAIL PROTECTED] (Alex Martelli)
  declaimed the following in comp.lang.python:

   for i in range(n):
   for j in range(i+1, n):
   print a[i], a[j]

  Ah, but wouldn't the cleaner Python be something like

   a = [1, 2, 3, 4, 5, 3, 6]   #just to confuse matters
   for pos, val in enumerate(a):
  ... for v2 in a[pos+1:]:
  ... print val, v2
  ...
  1 2
  1 3
  1 4
  1 5
  1 3
  1 6
  2 3
  2 4
  2 5
  2 3
  2 6
  3 4
  3 5
  3 3
  3 6
  4 5
  4 3
  4 6
  5 3
  5 6
  3 6

  --
  WulfraedDennis Lee Bieber   KD6MOG
  [EMAIL PROTECTED]   [EMAIL PROTECTED]
  HTTP://wlfraed.home.netcom.com/
  (Bestiaria Support Staff:   [EMAIL PROTECTED])
  HTTP://www.bestiaria.com/
  --
 http://mail.python.org/mailman/listinfo/python-list


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looping over lists

2007-05-05 Thread Paul Rubin
Dustan [EMAIL PROTECTED] writes:
  I think the for i in range() is more readable (Maybe because I'm
  coming from a c-style syntax language background) -  but what would
  the benefits of using enumerate be (other that being more . . .
  pythonesque?)
 
 It doesn't create a whole new list just for iterating.

That's what xrange is for.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looping over lists

2007-05-05 Thread Alex Martelli
Dustan [EMAIL PROTECTED] wrote:

 On May 5, 3:15 am, kaens [EMAIL PROTECTED] wrote:
  I think the for i in range() is more readable (Maybe because I'm
  coming from a c-style syntax language background) -  but what would
  the benefits of using enumerate be (other that being more . . .
  pythonesque?)
 
 It doesn't create a whole new list just for iterating.

As the example list was of length 5, that's not all that important in
this case.  In cases where it _is_ crucial, you can use xrange.

The performance of the various ways of looping is substantially the
same:

$ python -mtimeit -s'n=5; a=n*[23]' 'for i in range(n): x=a[i]'
100 loops, best of 3: 1.4 usec per loop
$ python -mtimeit -s'n=5; a=n*[23]' 'for i in xrange(n): x=a[i]'
100 loops, best of 3: 1.18 usec per loop
$ python -mtimeit -s'n=5; a=n*[23]' 'for i,v in enumerate(a): x=v'
100 loops, best of 3: 1.49 usec per loop
$ 

Here, xrange is minutely faster and enumerate slower, but each speed
difference in the noise.  Focusing on clarity is thus well warranted.

for i in range(n):
for j in range(i+1, n):
print a[i], a[j]
 
   Ah, but wouldn't the cleaner Python be something like
 
a = [1, 2, 3, 4, 5, 3, 6]   #just to confuse matters
for pos, val in enumerate(a):
   ... for v2 in a[pos+1:]:
   ... print val, v2

This breaks symmetry, by using enumerate in the outer loop and a slice
in the inner loop; the symmetrical construction of using range in both
loops is a big conceptual/clarity win -- the reader of the code needs to
grasp one fewer concept (locally).  Using xrange in both loops would
be just as good from this POV.


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looping over lists

2007-05-04 Thread Alex Martelli
Tommy Grav [EMAIL PROTECTED] wrote:

 I have a list:
 
a = [1., 2., 3., 4., 5.]
 
 I want to loop over a and then
 loop over the elements in a
 that is to the right of the current
 element of the first loop
 
 In C this would be equivalent to:
 
 for(i = 0; i  n; i++) {
 for(j=i+1; j  n; j++) {
   print a[i], a[j]
 
 and should yield:
 1.   2.
 1.   3.
 1.   4.
 1.   5.
 2.   3.
 2.   4.
 2.   5.
 3.   4.
 3.   5.
 4.   5.
 
 Can anyone help me with the right approach for this
 in python?

Closes to the C++ code would be:

for i in range(n):
for j in range(i+1, n):
print a[i], a[j]


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looping over lists

2007-05-04 Thread prad
On Friday 04 May 2007 18:40:53 Tommy Grav wrote:
 Can anyone help me with the right approach for this
 in python?

for each in a:
for item in a[a.index(each)+1:]:
print each,item

will produce 

1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

a.index(each) gives the index of the each value in the a list.
then you just add 1 to it so you start at the index value beside each's index.


-- 
In friendship,
prad

  ... with you on your journey
Towards Freedom
http://www.towardsfreedom.com (website)
Information, Inspiration, Imagination - truly a site for soaring I's
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looping over lists

2007-05-04 Thread Peter Otten
Tommy Grav wrote:

 I have a list:
 
a = [1., 2., 3., 4., 5.]
 
 I want to loop over a and then
 loop over the elements in a
 that is to the right of the current
 element of the first loop
 
 In C this would be equivalent to:
 
 for(i = 0; i  n; i++) {
 for(j=i+1; j  n; j++) {
 print a[i], a[j]
 
 and should yield:
 1.   2.
 1.   3.
 1.   4.
 1.   5.
 2.   3.
 2.   4.
 2.   5.
 3.   4.
 3.   5.
 4.   5.
 
 Can anyone help me with the right approach for this
 in python?

Two more options:

def pop_combine(items):
items = list(items)
while items:
a = items.pop(0)
for b in items:
print a, b

def enumerate_combine(items):
for i, a in enumerate(items):
for b in items[i+1:]:
print a, b

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list