472,805 Members | 747 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

enumerate() question

Hello!
I have a question for the developer[s] of enumerate(). Consider the
following code:

for x,y in coords(dots):
print x, y

When I want to iterate over enumerated sequence I expect this to work:

for i,x,y in enumerate(coords(dots)):
print i, x, y

Unfortunately, it doesn't =( and I should use (IMHO) ugly

for i,pair in enumerate(coords(dots)):
print i, pair[0], pair[1]

So, why enumerate() works this way and is there any chance of changing
the behaviour?

--
Regards, Gregory.

May 22 '06 #1
6 1986
Gregory Petrosyan a écrit :
Hello!
I have a question for the developer[s] of enumerate(). Consider the
following code:

for x,y in coords(dots):
print x, y

When I want to iterate over enumerated sequence I expect this to work:

for i,x,y in enumerate(coords(dots)):
print i, x, y

Unfortunately, it doesn't =( and I should use (IMHO) ugly
Try:

for i,(x,y) in enumerate(coords(dots)) :
...

for i,pair in enumerate(coords(dots)):
print i, pair[0], pair[1]

So, why enumerate() works this way and is there any chance of changing
the behaviour?


No chance, behavior is correct.

A+

L.Pointal.
May 22 '06 #2
Gregory Petrosyan wrote:
Hello!
I have a question for the developer[s] of enumerate(). Consider the
following code:

for x,y in coords(dots):
print x, y

When I want to iterate over enumerated sequence I expect this to work:

for i,x,y in enumerate(coords(dots)):
print i, x, y

Unfortunately, it doesn't =( and I should use (IMHO) ugly

for i,pair in enumerate(coords(dots)):
print i, pair[0], pair[1]
Use:

for i, (x, y) in enumerate(coords(dots)):
print i, x, y
So, why enumerate() works this way and is there any chance of changing
the behaviour?


Because enumerate has no way to distinguish between iterables you do and
don't want unpacked. So, for example, this wouldn't work under your
proposal:

for index, string in ["foo", "bar", "baz"]:
print "String number %s is %s." % (index, string)

But this would:

for index, x, y, z in ["foo", "bar", "baz"]:
print "First character of string number %s is %s." % (index, x)
May 22 '06 #3
> I have a question for the developer[s] of enumerate(). Consider the
following code:

for x,y in coords(dots):
print x, y

When I want to iterate over enumerated sequence I expect this to work:

for i,x,y in enumerate(coords(dots)):
print i, x, y


for i, (x, y) in enumerate(coords(dots)):
print i, x, y

--
damjan
May 22 '06 #4
Gregory Petrosyan wrote:
Hello!
I have a question for the developer[s] of enumerate(). Consider the
following code:

for x,y in coords(dots):
print x, y

When I want to iterate over enumerated sequence I expect this to work:

for i,x,y in enumerate(coords(dots)):
print i, x, y

Unfortunately, it doesn't =( and I should use (IMHO) ugly

for i,pair in enumerate(coords(dots)):
print i, pair[0], pair[1]

So, why enumerate() works this way and is there any chance of changing
the behaviour?


It works that way because enumerate returns a tuple - index, value. And it
doesn't care what is inside value. Actually, it can't - how would you then
write something like this?

l = [1, ('a', 'tuple'), 3]

for i, value in enumerate(l):
print i, value
But your problem can be solved in an elegant fashion anyway. When *you* know
the structure of the values (and who else does?), you can simply use nested
sequence unpacking:

for i, (x, y) in enumerate(coords):
pass

HTH,

Diez

May 22 '06 #5
Thanks a lot.

May 22 '06 #6
Gregory Petrosyan wrote:
Hello!
I have a question for the developer[s] of enumerate(). Consider the
following code:

for x,y in coords(dots):
print x, y

When I want to iterate over enumerated sequence I expect this to work:

for i,x,y in enumerate(coords(dots)):
print i, x, y

Unfortunately, it doesn't =( and I should use (IMHO) ugly

for i,pair in enumerate(coords(dots)):
print i, pair[0], pair[1]

So, why enumerate() works this way
Special cases aren't special enough to break the rules.

enumerate shouldn't behave differently for different types of items.
and is there any chance of changing
the behaviour?


No, not really.

Instead, try this:

for i, (x, y) in enumerate(coords(dots)):
print i, x, y

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

May 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pekka Niiranen | last post by:
Hi, I have Perl code looping thru lines in the file: line: while (<INFILE>) { ... $_ = do something ... if (/#START/) { # Start inner loop
4
by: Phillip N Rounds | last post by:
I need to eumerate all available drives in a C# Windows Form application ( and then of course the directory tree, but that's not the problem) My question is, how to I enumerate all the available...
5
by: Gaetan | last post by:
I would like to guarantee that only one session at a time can request exclusive access to an object stored in Application. The ownership of the object must last throughout multiple HTTP requests. ...
1
by: smichr | last post by:
I see that there is a thread of a similar topic that was posted recently ( enumerate with a start index ) but thought I would start a new thread since what I am suggesting is a little different. ...
2
by: eight02645999 | last post by:
hi, i am using python 2.1. Can i use the code below to simulate the enumerate() function in 2.3? If not, how to simulate in 2.1? thanks from __future__ import generators def...
8
by: Dustan | last post by:
Can I make enumerate(myObject) act differently? class A(object): def __getitem__(self, item): if item 0: return self.sequence elif item < 0: return self.sequence elif item == 0: raise...
21
by: James Stroud | last post by:
I think that it would be handy for enumerate to behave as such: def enumerate(itrbl, start=0, step=1): i = start for it in itrbl: yield (i, it) i += step This allows much more flexibility...
12
by: Danny Colligan | last post by:
In the following code snippet, I attempt to assign 10 to every index in the list a and fail because when I try to assign number to 10, number is a deep copy of the ith index (is this statement...
8
by: eight02645999 | last post by:
hi say i want to enumerate lines of a file eg for n,l in enumerate(open("file")): # print next line ie is there a way to print out the next line from current line using the above?. Or do i...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.