Multi-key Python dictionary

September 17th, 2011 by erb

There are several (easily found via Google) Python dictionary variations available, that allow you to map multiple values for a given key.  Essentially allowing a list of values for the key to be stored.

I was looking for the converse: I wanted to (efficiently) hash multiple keys to a single, common value.

Here’s code to do this (after the cut), that provides a (relatively) complete equivalent to the built-in dict methods: (minimally tested!)

>>> d = MultiDict({'a':2, 'b':3})
>>> d
{'a': 2, 'b': 3}
>>> d['c'] = 4 ; d
{'a': 2, 'b': 3, 'c': 4}
>>> d.setdefault(['x','a'])
>>> d
{['a', 'x']: 2, 'b': 3, 'c': 4}
>>> d['a']
>>> d['x'] = 10 ; d
{['a', 'x']: 10, 'b': 3, 'c': 4}
>>> d = MultiDict([['a', 10], [['b','c','d'], 20], ['e',30]])
>>> d
{'a': 10, ['c', 'b', 'd']: 20, 'e': 30}
>>> d['b']
>>> d['d'] = 50 ; d['c']
>>> d.keys()
['a', 'c', 'b', 'e', 'd']
>>> d.values()
[10, 50, 50, 30, 50]
>>> d.items()
[('a', 10), ('c', 50), ('b', 50), ('e', 30), ('d', 50)]
>>> d.__sizeof__()
>>> 'b' in d
>>> del d['b']
>>> 'b' in d
>>> d
{'a': 10, ['c', 'd']: 50, 'e': 30}
>>> d.pop('c')
>>> d.pop('d')
>>> d
{'a': 10, 'e': 30}
>>> d.__sizeof__()
>>> d.setdefault(['a','b'], 60)
>>> d
{['a', 'b']: 60, 'e': 30}

Code: Read the rest of this entry »

More Mathematica Fun for Memorial Day

May 30th, 2011 by erb

Mathematica Users LinkedIn contributor Sol Lederman posted a Mathematica Programming Contest on his Playing with Mathematica blog this weekend for Memorial Day:

Draw the US Flag in Mathematica. To be eligible to win you should get the colors roughly right as well as the number of stars and stripes. I’ll give extra points if your flag has creases in it, as if it were flapping in the wind. I’ll give double extra brownie points if you create an animated GIF of your Mathematica US flag flapping in the wind.

US Flag

So I did just that!

The Mathematica notebook is available, as is the Computable Document Format file, and a PDF file.

(CDF files are a new format from Wolfram Research, allowing you to actually load and interact with Mathematica notebooks on your own computer, with the free downloadable CDF Player.)

Holiday 2009 Card Photos

December 20th, 2009 by erb

Ripley and Bogart…

Ripley and Bogart #1

Ripley and Bogart #2

Ripley and Bogart #3

Ah the 90s…

October 26th, 2008 by erb

I was unpacking this weekend, and came across my old college yearbooks. I just had to scan some pictures to post … some for (misplaced?) pride, some just for shame!

1991 RHIT Yearbook 1991 Theta Xi
1991 RHIT Yearbook, Theta Xi Composite

1992 RHIT Yearbook 1992 Theta Xi
1992 RHIT Yearbook, Theta Xi Composite

Greek Games
Greek Games “The Taxi” cart (circa 1991?)

Sisters of Mercy on Tour

September 9th, 2008 by erb

So I got an email from TicketsWest saying Sisters concert tickets were going on Internet Pre-Sale tomorrow morning.

Saturday Nov. 22nd, 2008, Roseland Theater (Portland)
Sunday Nov. 23rd, 2008, El Corazon (Seattle)

Anyone have the skinny on Tour dates and locations? Specifically, Mid-West? The Sisters web site is of no help….

Monthly Game Night

September 1st, 2008 by erb

CraniumOnce a month, come over for an evening of board games, food, drink, and fun at ChezBrodeur! Hang with old friends, make new friends, and relax playing board games.

2nd Saturday of every month at ChezBrodeur.  All the details are at the site. First night is on Facebook if you want to RSVP.