473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 2015
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.