2

The data structure should meet the following purpose:

  • each object is unique with certain key-value pairs
  • the keys and values are not predetermined and can contain any string value
  • querying for objects should be fast

Example:

  • object_123({'stupid':True, 'foo':'bar', ...})
  • structure.get({'stupid':True, 'foo':'bar', ...}) should return object_123

Optimally this structure is implemented with the standard python data structures available through the standard library.

How would you implement this?

5
  • @phooji: No. I just couldn't think of any could clean and good implementation of this and was looking for expert advice. Commented Apr 6, 2011 at 19:47
  • @phooji: I wondered that, too, but his other questions seem legit. Commented Apr 6, 2011 at 19:48
  • Are the objects mutable? Can object_123 get new keys or changed values or both? Commented Apr 6, 2011 at 19:49
  • @S.Lott: in this specific case they are immutable, but it would be interesting, how this is implemented with mutable objects? Commented Apr 6, 2011 at 19:52
  • @ahojnnes,@Kirk Strauser: Thanks -- just clarifying. Commented Apr 6, 2011 at 21:14

3 Answers 3

6

The simplest solution I can think of is to use sorted tuple keys:

def key(d): return tuple(sorted(d.items()))

x = {}
x[key({'stupid':True, 'foo':'bar', ...})] = object_123

x.get(key({'stupid':True, 'foo':'bar', ...})) => object_123

Another option would be to come up with your own hashing scheme for your keys (either by wrapping them in a class or just using numeric keys in the dictionary), but depending on your access pattern this may be slower.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, although part of me is wondering if there is a way that namedtuple might help...
0

I think SQLite or is what you need. It may not be implemented with standard python structures but it's available through the standard library.

Comments

0

Say object_123 is a dict, which it pretty much looks like. Your structure seems to be a standard dict with keys like (('foo', 'bar'), ('stupid', True)); in other words, tuple(sorted(object_123.items())) so that they're always listed in a defined order.

The reason for the defined ordering is because dict.items() isn't guaranteed to return a list in a given ordering. If your dictionary key is (('foo', 'bar'), ('stupid', True)), you don't want a false negative just because you're searching for (('stupid', True),('foo', 'bar')). Sorting the values is probably the quickest way to protect against that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.