Re: A little more advanced for loop

2007-02-10 Thread Aahz
In article <[EMAIL PROTECTED]>,
Larry Bates  <[EMAIL PROTECTED]> wrote:
>
>Note: if lists are long take a look at itertools izip.  zip creates
>a list of lists which could take lots of memory/time if they are VERY
>large.  itertools izip iterates over them in place.

That's half-true -- while izip is faster, it's not enough faster to make
a significant difference unless the lists are "huge" rather than
"large".  Remember that the data elements themselves are not copied.
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

"I disrespectfully agree."  --SJM
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A little more advanced for loop

2007-02-10 Thread Duncan Booth
Larry Bates <[EMAIL PROTECTED]> wrote:

> Note: if lists are long take a look at itertools izip.  zip creates
> a list of lists which could take lots of memory/time if they are VERY
> large.  itertools izip iterates over them in place.

That's interesting. I was going to quibble with the assertion that zip 
could take lots of time, since on the face of it a loop using izip packs 
and unpacks just as many tuples. Fortunately I tried it out before claiming 
that zip would be just as fast :)

It would appear that even for short sequences the izip solution is faster. 
My guess would be it is because the same tuple objects are being reused in 
the izip version.

C:\Python25\Lib>timeit.py -s "a, b, c = [range(1000)]*3" -s "from itertools 
import izip" "for p,q,r in izip(a,b,c): pass"
1 loops, best of 3: 131 usec per loop

C:\Python25\Lib>timeit.py -s "a, b, c = [range(1000)]*3" "for p,q,r in 
zip(a,b,c): pass"
1000 loops, best of 3: 212 usec per loop

C:\Python25\Lib>timeit.py -s "a, b, c = [range(100)]*3" -s "from itertools 
import izip" "for p,q,r in izip(a,b,c): pass"

10 loops, best of 3: 13.9 usec per loop

C:\Python25\Lib>timeit.py -s "a, b, c = [range(100)]*3" "for p,q,r in 
zip(a,b,c): pass"
1 loops, best of 3: 22.6 usec per loop

C:\Python25\Lib>timeit.py -s "a, b, c = [range(10)]*3" -s "from itertools 
import izip" "for p,q,r in izip(a,b,c): pass"
10 loops, best of 3: 2.21 usec per loop

C:\Python25\Lib>timeit.py -s "a, b, c = [range(10)]*3" "for p,q,r in 
zip(a,b,c): pass"
10 loops, best of 3: 3.52 usec per loop
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A little more advanced for loop

2007-02-09 Thread Larry Bates
Horta wrote:
> On Feb 9, 9:00 am, Stephan Diehl <[EMAIL PROTECTED]> wrote:
>> Horta wrote:
>>> Hi folks,
>>>   Suppose I have to loop over 3 lists being the same size at the same
>>> time and order. How can I do that without using the range() function
>>> or whatever indexing?
>>> Example using range:
>>> a = ['aaa', '']
>>> b = ['bb', '']
>>> c = ['c', '']
>>> for i in range(len(a)):
>>> # using a[i], b[i], and c[i]
>>>   I'm sure there's a elegant way to do that...
>>>   Thanks in advance.
>> Sure, there is:
>>
>> for a_item, b_item , c_item in zip(a,b,c):
>> # do something
> 
>   Thanks guys!
> 
Note: if lists are long take a look at itertools izip.  zip creates
a list of lists which could take lots of memory/time if they are VERY
large.  itertools izip iterates over them in place.

from itertools import izip

for a_item, b_item, c_item in izip(a,b,c):
# do something

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


Re: A little more advanced for loop

2007-02-09 Thread Horta
On Feb 9, 9:00 am, Stephan Diehl <[EMAIL PROTECTED]> wrote:
> Horta wrote:
> > Hi folks,
>
> >   Suppose I have to loop over 3 lists being the same size at the same
> > time and order. How can I do that without using the range() function
> > or whatever indexing?
>
> > Example using range:
>
> > a = ['aaa', '']
> > b = ['bb', '']
> > c = ['c', '']
>
> > for i in range(len(a)):
> > # using a[i], b[i], and c[i]
>
> >   I'm sure there's a elegant way to do that...
>
> >   Thanks in advance.
>
> Sure, there is:
>
> for a_item, b_item , c_item in zip(a,b,c):
> # do something

  Thanks guys!

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


Re: A little more advanced for loop

2007-02-09 Thread Stephan Diehl
Horta wrote:
> Hi folks,
> 
>   Suppose I have to loop over 3 lists being the same size at the same
> time and order. How can I do that without using the range() function
> or whatever indexing?
> 
> Example using range:
> 
> a = ['aaa', '']
> b = ['bb', '']
> c = ['c', '']
> 
> for i in range(len(a)):
> # using a[i], b[i], and c[i]
> 
>   I'm sure there's a elegant way to do that...
> 
>   Thanks in advance.
> 
Sure, there is:

for a_item, b_item , c_item in zip(a,b,c):
# do something
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A little more advanced for loop

2007-02-09 Thread Diez B. Roggisch
Horta wrote:

> Hi folks,
> 
>   Suppose I have to loop over 3 lists being the same size at the same
> time and order. How can I do that without using the range() function
> or whatever indexing?
> 
> Example using range:
> 
> a = ['aaa', '']
> b = ['bb', '']
> c = ['c', '']
> 
> for i in range(len(a)):
> # using a[i], b[i], and c[i]
> 
>   I'm sure there's a elegant way to do that...


Use zip:

for av, bv, cv in zip(a, b, c):
print av, bv, cv

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