Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: for in sequence problem... possible new operator to add to python

Reply
Thread Tools

RE: for in sequence problem... possible new operator to add to python

 
 
sismex01@hebmex.com
Guest
Posts: n/a
 
      07-10-2003
> From: Peter Hansen [mailto(E-Mail Removed)]
> Sent: Jueves, 10 de Julio de 2003 11:36 a.m.
>
> Adam Gent wrote:
> > [..snippage..]

>
> [..more.snippage..]
>
> When you iterate over a dict in recent versions of Python, you
> are by definition iterating over the keys in the dict. If you
> want the values, you use .values(), and if you want both keys
> and values, you use .items(). See the docs for more.
>
> -Peter
>


Actually, .keys(), .values() and .items() return their respective
lists, in arbitrary order.

If you wish to use an iterator, use .iterkeys() , .itervalues()
or .iteritems() ; very helpful in the case of big dictionaries,
since you don't need to create and then destroy big lists.

Adam: If you wish to iterate through the items by default,
have you tried something like this?

class NewDict(dict):
def __iter__(self):
return super(NewDict,self).itervalues()

That should, by default (like "for x in nd:") iterate through
the values of a dictionary, instead of it's keys.

I haven't tested it though, so caveat emptor.

-gustavo
Advertencia:La informacion contenida en este mensaje es confidencial y
restringida, por lo tanto esta destinada unicamente para el uso de la
persona arriba indicada, se le notifica que esta prohibida la difusion de
este mensaje. Si ha recibido este mensaje por error, o si hay problemas en
la transmision, favor de comunicarse con el remitente. Gracias.

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      07-10-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> > From: Peter Hansen [mailto(E-Mail Removed)]
> > When you iterate over a dict in recent versions of Python, you
> > are by definition iterating over the keys in the dict. If you
> > want the values, you use .values(), and if you want both keys
> > and values, you use .items(). See the docs for more.

>
> Actually, .keys(), .values() and .items() return their respective
> lists, in arbitrary order.


I realize that. I didn't mean to imply anything different.

> If you wish to use an iterator, use .iterkeys() , .itervalues()
> or .iteritems() ; very helpful in the case of big dictionaries,
> since you don't need to create and then destroy big lists.


Sorry, perhaps I should stop using the term "iterate" for its
more widely known generic meaning of visiting each item in a
sequence one at a time, and restrict my usage only to those
cases where in Python I'm talking about an actual "iterator"
object.

("Iterate" was a very general term... it would be a shame if
one could no longer use it as such.)

-Peter
 
Reply With Quote
 
 
 
 
John J. Lee
Guest
Posts: n/a
 
      07-11-2003
Peter Hansen <(E-Mail Removed)> writes:

> (E-Mail Removed) wrote:
> >
> > > From: Peter Hansen [mailto(E-Mail Removed)]
> > > When you iterate over a dict in recent versions of Python, you

[...]
> Sorry, perhaps I should stop using the term "iterate" for its
> more widely known generic meaning of visiting each item in a
> sequence one at a time, and restrict my usage only to those
> cases where in Python I'm talking about an actual "iterator"
> object.
>
> ("Iterate" was a very general term... it would be a shame if
> one could no longer use it as such.)


I think you're correct even in the strict Python sense of the word
'iteration'. Objects (eg. dicts and lists) that are not themselves
iterators can still support iteration. Section 2.2.5 from the 2.2
library reference:

Python defines several iterator objects to support iteration over
general and specific sequence types, dictionaries, and other more
specialized forms. ...


John
 
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
What are the key differences between operator new and operator new[]? xmllmx C++ 6 02-03-2010 04:11 PM
how to iterate over sequence and non-sequence ? stef mientki Python 13 10-20-2007 10:21 AM
Problem with Range Operator sequence number Paul Porcelli Perl 3 07-16-2004 02:41 AM
Problem with Range Operator sequence number Paul Porcelli Perl 1 07-14-2004 10:52 PM
BOOT SEQUENCE (how to change boot sequence) bird Computer Support 13 12-24-2003 02:20 AM



Advertisments