45 lines
1.2 KiB
Plaintext
45 lines
1.2 KiB
Plaintext
From: mhagger at blizzard.harvard.edu (Michael Haggerty)
|
|
Date: 15 May 1999 17:54:24 -0400
|
|
Subject: `Parallel' lists (index and value of list)
|
|
References: <000a01be9f04$d3c2d800$659e2299@tim> <373DCED3.53BB1A9A@home.com>
|
|
Message-ID: <xj0lneqrobz.fsf_-_@cyclone.harvard.edu>
|
|
X-UID: 1969
|
|
|
|
> The latter is what Python lacks today, and the source of the frequent
|
|
>
|
|
> for i in range(len(seq)):
|
|
> element = seq[i]
|
|
>
|
|
> idiom.
|
|
|
|
What if lists had a `values' member like dictionaries do? Then you
|
|
could write
|
|
|
|
for (i,element) in seq.values():
|
|
print 'Element %d is %s.' % (i, element)
|
|
|
|
seq.values() would return a list of (index, value) tuples just as it
|
|
does for dictionaries (of course for lists they would be in order).
|
|
Its implementation would be equivalent to
|
|
|
|
class UserList:
|
|
...
|
|
def values(self):
|
|
return map(None, range(len(l)), l)
|
|
|
|
The advantage is that it requires no new syntax in the language, just
|
|
an additional member function for lists which is intuitively analogous
|
|
to a member function already present for dictionaries. It might even
|
|
be possible to optimize this construct when used in a for statement.
|
|
|
|
Just a thought...
|
|
Michael
|
|
|
|
--
|
|
Michael Haggerty
|
|
mhagger at blizzard.harvard.edu
|
|
|
|
|
|
|
|
|