Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > any such thing as list interleaving?

Reply
Thread Tools

any such thing as list interleaving?

 
 
Tom Plunket
Guest
Posts: n/a
 
      07-12-2003

I find myself often doing the following sort of thing (sorry for
lack of whitespace, I don't want the line to break):

for entry, index in map(lambda e,ie,i),aList,range(len(aList)):
# ...

This definitely seems like a roundabout way to loop through
parallel lists together. Is this map routine truly the easiest/
best/most straight-forward way to do a for loop through parallel
lists, if I feel that the Python anti-idom of:

for index in range(len(myList)):
entry = aList(index)
anotherEntry = anotherList(index)
# ...

???

This also brings up a similar problem for me when iterating over
dictionaries:

for key in myDict:
value = myDict[key]
# ...

This seems a pretty sloppy way to go about it, imo. There must
be something more in the Python spirit!

Thanks.

-tom!
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      07-12-2003
Tom Plunket wrote:

> I find myself often doing the following sort of thing (sorry for
> lack of whitespace, I don't want the line to break):
>
> for entry, index in map(lambda e,ie,i),aList,range(len(aList)):
> # ...


In Python 2.3, you can write

for index, entry in enumerate(L):
# ...

For 2.2, you can define enumerate yourself:

def enumerate(L):
i = 0
while 1:
try:
yield i, L[i]
except IndexError:
return
i += 1

For older versions, yet another definition would be needed;
I leave that as an exercise.

> This also brings up a similar problem for me when iterating over
> dictionaries:
>
> for key in myDict:
> value = myDict[key]
> # ...
>
> This seems a pretty sloppy way to go about it, imo. There must
> be something more in the Python spirit!


Here, you could always write

for key, value in myDict.items():
#...

Since 2.2, there is another method available which does not create
a list of tuples:

for key, value in myDict.iteritems():
#...

HTH,
Martin


 
Reply With Quote
 
 
 
 
Gonšalo Rodrigues
Guest
Posts: n/a
 
      07-12-2003
On Sat, 12 Jul 2003 14:00:49 -0700, Tom Plunket <(E-Mail Removed)>
wrote:

>
>I find myself often doing the following sort of thing (sorry for
>lack of whitespace, I don't want the line to break):
>
>for entry, index in map(lambda e,ie,i),aList,range(len(aList)):
> # ...
>
>This definitely seems like a roundabout way to loop through
>parallel lists together. Is this map routine truly the easiest/
>best/most straight-forward way to do a for loop through parallel
>lists, if I feel that the Python anti-idom of:
>


Check the zip builtin:

>>> help(zip)

Help on built-in function zip:

zip(...)
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

Return a list of tuples, where each tuple contains the i-th
element
from each of the argument sequences. The returned list is
truncated
in length to the length of the shortest argument sequence.


>for index in range(len(myList)):
> entry = aList(index)
> anotherEntry = anotherList(index)
> # ...
>
>???
>
>This also brings up a similar problem for me when iterating over
>dictionaries:
>
>for key in myDict:
> value = myDict[key]
> # ...
>


Fire the interpreter and type:

>>> help(dict)


As it's a long stretch of text, I'll just post the relevant part:

| iteritems(...)
| D.iteritems() -> an iterator over the (key, value) items of D
|

>This seems a pretty sloppy way to go about it, imo. There must
>be something more in the Python spirit!
>
>Thanks.
>
>-tom!


With my best regards,
G. Rodrigues
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
any such thing as a broadband monitor program? fred Computer Support 19 02-25-2005 02:13 AM
Is there such a thing as a JVM monitor... Edward A Thompson Java 3 10-02-2003 10:32 PM
ScrollBar Buttons? - Any such thing? David Crichton HTML 4 09-02-2003 04:34 PM



Advertisments