Unanswered Questions in python.faqts.com

Charles Hixson charleshixsn at earthlink.net
Wed Aug 23 11:18:26 EDT 2000


That's a good answer to the question.  Unfortunately, the FAQTS question
format caused an oversimplified question.  What I was looking for was some
way of implementing a data structure that had both sliced and keyed access.
Envisions a sorted tree as the underlying data format, with insertion only
possible by key.  This is almost the same as a dictionary, but would permit
sliced access as well as individual item retrieval.  Actually, my ideal form
of the answer would allow a database to be merged into the language with
btree access as the underlying format, and nominal form looking like a cross
between a dictionary and a sequence.  I don't really want to slice on record
#, but rather on key value (like a between query).

I know that this is possible via subroutine calls, but I was wondering if
there were any way that made more direct use of the preexisting syntax,
e.g.:  abrasax = litmus {"acid" : "base"} or some such.



Will Ware wrote:

> Fiona Czuczman (fiona at sitegnome.com) wrote:
> > FAQTS : Computers : Programming : Languages : Python : Language and
> > Syntax
> > How should a sortable sequence be implemented?
> > http://www.faqts.com/knowledge-base/view.phtml/aid/5367/fid/241/lang/
>
> Objects can be sorted as long as they can be compared. Fundamental
> types (ints, floats, strings) can be compared with the globally defined
> cmp() function. cmp(x,y) returns a negative integer if x<y, zero if x==y,
> and a positive integer if x>y. If the 'sort' method is called on a list
> of objects, the list will be sorted in increasing order:
>
> x = [3,1,4,1,5,9,2,6]
> x.sort()
> print x  -->  [1,1,2,3,4,5,6,9]
>
> Classes can be made sortable by defining a __cmp__ method for them. For
> example, to sort a list of 3-d points in decreasing order of z, you could
> write:
>
> class Point:
>     def __init__(self, x,y,z):
>         self.x, self.y, self.z = x, y, z
>     def __cmp__(self, other):
>         return -cmp(self.z, other.z)
>         # or: return cmp(other.z, self.z)
>
> Now if you create a list of points (x) and call x.sort(), the points will
> be sorted in decreasing-z order.
>
> Another way to define the sorting criterion is to pass a cmp-like function
> as the argument to x.sort(). To re-sort your points in increasing-y order,
> you can write:
>
> x.sort(lambda self,other: cmp(self.y, other.y))
>
> or if you don't like lambdas:
>
> def y_sorter(u,v):
>     return cmp(u.y, v.y)
> x.sort(y_sorter)
>
> --
> # - - - - - - - - - - - - - - - - - - - - - - - -
> # Resistance is futile. Capacitance is efficacious.
> # Will Ware     email:    wware @ world.std.com

-- (c) Charles Hixson
--  Addition of advertisements or hyperlinks to products specifically
prohibited

-------------- next part --------------
A non-text attachment was scrubbed...
Name: charleshixsn.vcf
Type: text/x-vcard
Size: 153 bytes
Desc: Card for Charles Hixson
URL: <http://mail.python.org/pipermail/python-list/attachments/20000823/31548281/attachment.vcf>


More information about the Python-list mailing list