Slides from last night's Meeting: More ordered dict ideas

1 view
Skip to first unread message

Rich

unread,
Nov 10, 2009, 11:50:47 AM11/10/09
to Tucson Python Language Enthusiasts, RichIsMy...@yahoo.com, d...@rincon.com
Here are my slides from last night's Tuple meeting about dictionaries
in Python 3.0

I appreciate everyone's feedback: Should (1) ['a':1, 'b':2, 'c':3] be
the ordered dictionary
literal or should (2) o{'a':1, 'b':2, 'c':3}

I am still not sure what I like. I like the fact that the [] notation
completes the loop:

{} []
: dict ordereddict
, set list

But I also like the little o out front (like r in raw strings: r'123')
giving different types of tables.
I can imagine at least one more relevant type of dictionary: the
sorted dictionary
(corresponding to the STL map):

sorted_dict = s{ 'aaa':2, 'bbb':3 }
for x in sorted_dict : # Keeps table in sorted order and always
iterates in sorted order
print(x)

ordered_dict = o{'aaa'1, 'g':2, 'b':3}
for x in ordered_dit : # Iterates in insertion order, not
necessarily sorted
print(x)

plain_dict = { 'a':1, 'b':2 }
for x in plain_dict : # Iterates through in hash order
print(x)

What if we closed the loop and use little letters for literal lists
too?
Image a list that is held in sorted order:

sorted_list = s[ 6,4,3] # Sorts as it creates, so literal doesn't
have to be sorted

Meh, I am less in love with the s for the sorted list ...
But, we could have literals for data structures which supported multi-
ness

plain_set = { 1,2,3 }
multiset = m{ 1,2,3,3} # Allows duplicates

multi_dict = m{ 'a':1, 'a':2 } # Allows duplicates of keys

Thoughts?

Gooday,

Richie

Rich

unread,
Nov 10, 2009, 11:54:19 AM11/10/09
to Tucson Python Language Enthusiasts
Forgot the slides ... ;)
http://www.picklingtools.com/TupleDict.pdf

Gooday,

Richie

Scott Schanel

unread,
Nov 11, 2009, 9:41:27 PM11/11/09
to Tucson Python Language Enthusiasts
From my subjective viewpoint, both seem to have merit, but if I were
you I'd be leaning towards the ['a':1, 'b':2] notation.

Re: the 'sorted' varieties: With the new way of iterating through
dictionary keys, I'm not sure why it would be necessary to create new
data structures for sorted lists and dictionaries when it is just as
easy to do the following:

for key in sorted(mydict.keys()):

If this turns out not to be sufficient for performance reasons, you
could always implement a SortedKeysView. Maybe that would help.
Maybe not.

There could also be confusion because vanilla dictionary keys can be
any hashable type, and list elements can be any type.

mydict = { 1: 2, 'hello': [4, 6] }
mylist = [1, 2, 'hello', { 4: 5, 'goodbye': 'hello' }]

The sorted versions would more restrictive in that the types would
need to be orderable, so I think it would be deceptive to the user
(programmer) to try so hard to maintain a similar literal
representation. But hey I'm biased because I like things to be
explicit. And it doesn't really solve the original problem, which was
to eliminate the need to create the ordered dictionary using all the
parentheses and brackets in this line:

OrderedDict([(key1, value1), (key2, value2)])

Re: the 'multi' varieties:

Creating a multiset using something like

multiset((1, 2, 3, 4, 4, 5, 5, 5))

and a multimap/multidict using

multimap({ key1: [ value11, value12 ], key2: [value21, value22] })

doesn't seem all that bad to me. But like I said, I have biases, er,
I mean preferences. (Yeah, "preferences", that's it.)

BTW multimaps in STL are more or less implemented as plain ol' maps
where the values are vectors (lists), like above, with some additional
methods.

- Scott

P.S. Using the 'o' prefix and 'm' prefix, what if you had an ordered
multimap? You'd have to have 'om' or 'mo' before the braces, and now
it's just starting to get weird.

On Nov 10, 9:50 am, Rich <richismyn...@gmail.com> wrote:

Lucas Taylor

unread,
Nov 12, 2009, 3:12:32 AM11/12/09
to tuple...@googlegroups.com
On 11/10/09 9:50 AM, Rich wrote:
> Here are my slides from last night's Tuple meeting about dictionaries
> in Python 3.0
>
> I appreciate everyone's feedback: Should (1) ['a':1, 'b':2, 'c':3] be
> the ordered dictionary
> literal or should (2) o{'a':1, 'b':2, 'c':3}
>
> I am still not sure what I like. I like the fact that the [] notation
> completes the loop:
>
> {} []
> : dict ordereddict
> , set list
>

At first #1 mades the most immediate sense to me: [] indicates an
ordered sequence and the k:v syntax indicates a mapping. Seems easy
enough to explain and understand. It would be nicer in the shell.

But, it seems like I mostly find myself building mappings at run-time,
so instantiating an empty OrderedDict and working with it doesn't seem
too big of a deal.

And if you are doing something where you want to instantiate it with a
bunch of data...are you really going to be typing in more than a few
pairs before you get disgusted and work out a way to automate it?

Which is pretty easy:

keys = ('a','b','c','d')
vals = (1,2,3,4)
odict = OrderedDict(zip(keys, vals))



Lucas
> --
>
> You received this message because you are subscribed to the Google Groups "Tucson Python Language Enthusiasts" group.
> To post to this group, send email to tuple...@googlegroups.com.
> To unsubscribe from this group, send email to tuplegroup+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/tuplegroup?hl=.
>
>
>


Reply all
Reply to author
Forward
0 new messages