Mailing List Archive

sliciing dicts (was RE: More random python observations from a perl programmer)
[tchrist]
>> GOTCHA: (medium)
>> Slices in Python must be contiguous ranges of a sequence.
>> In Perl, there's no such restriction.

[Michael Hudson]
> At some point Python will probably support `extended slice syntax'
> like so:
>
> range(0,10)[1:4:2] => [1,3]
>
> Numerical Python already does this.

[tchrist]
>> GOTCHA: (medium)
>> You can't slice dictionaries at all in Python. In Perl, it's
>> easy to slice a hash, and just as sensible.

[Michael Hudson]
> Huh? What would that mean? There's no reason to suspect that the keys
> of a dictionary are ordered.

What Tom calls "slicing" here the NumPy folk would call gather/scatter
subscripting. A list can be used as an index, e.g.

thing[[7,2,3]] == [thing[7], thing[2], thing[3]]

Perl has something like that; Python does not.

can't-tell-the-subscripting-modes-without-a-scorecard-ly y'rs - tim
sliciing dicts (was RE: More random python observations from a perl programmer) [ In reply to ]
In article <001301beeacf$9819a560$712d2399@tim>,
"Tim Peters" <tim_one@email.msn.com> wrote:
> [tchrist]
> >> GOTCHA: (medium)
> >> Slices in Python must be contiguous ranges of a sequence.
> >> In Perl, there's no such restriction.
>
> [Michael Hudson]
> > At some point Python will probably support `extended slice syntax'
> > like so:
> >
> > range(0,10)[1:4:2] => [1,3]
> >
> > Numerical Python already does this.
>
> [tchrist]
> >> GOTCHA: (medium)
> >> You can't slice dictionaries at all in Python. In Perl, it's
> >> easy to slice a hash, and just as sensible.
>
> [Michael Hudson]
> > Huh? What would that mean? There's no reason to suspect that the
keys
> > of a dictionary are ordered.
>
> What Tom calls "slicing" here the NumPy folk would call gather/scatter
> subscripting. A list can be used as an index, e.g.
>
> thing[[7,2,3]] == [thing[7], thing[2], thing[3]]
>
> Perl has something like that; Python does not.

All of these things of course can be implemented using the
various python meta-hooks either at the Python or the C level.
They aren't intrinsic limitations of the Python interpreter,
just limitations of the standard dictionary/list/tuple
implementations.

BTW, kjbuckets.kjDict's have a method called "dump" (with a dual
kjUndump function) where

D.dump((x,)) --> D[x]
D.dump((x,y,z)) --> (D[x], D[y], D[z])

Gadfly uses these features to quickly pack/unpack data in/out
of dictionary format either for indexing or archiving.

I really think you should get kjbuckets, Tom, it sounds like it's just
what you need for the Python app you are apparently working on. :)

Aaron Watters http://www.chordate.com

===

I forgot my mantra.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.