3

This question is the python version of: Is there a Collection that works like a Dictionary without the values?

I want a data structure that has a list of english words in it, but not their definitions.

Basically: given a sequence of letters, I want to be able to do constant-time O(1) lookup to determine if that sequence is in the english dictionary.

Would a set() or frozenset() be the correct choice?

I know that I could use a dictionary where each key's value is None, but that seems like a waste of memory space.

Community
  • 1
  • 1
zzz
  • 2,515
  • 4
  • 28
  • 38
  • 2
    What do you mean by _given a sequence of letters?_ Assuming there is a word `apocalypse` in your dictionary, do you expect to get true by looking up `apoca` or `lypse`? – tomasz Mar 07 '12 at 23:38

4 Answers4

4

Yes, set is the right tool for this job. You can find out whether a word is in the set with in, which operates in O(1) time. Adding words is done with the add member which takes amortized O(1) time. It additionally has all the usual finite-set operations: union, intersection, difference, etc.:

>>> A = set(["foo", "bar", "baz"])
>>> B = set(["foo", "ham", "spam"])
>>> "foo" in A
True
>>> "bar" in B
False
>>> A | B
set(['bar', 'ham', 'spam', 'foo', 'baz'])
>>> A & B
set(['foo'])
>>> A - B
set(['bar', 'baz'])
>>> B - A
set(['ham', 'spam'])
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
1

Yes. Set lookup is O(1) in the average case, much to my surprise honestly. The implementation is supposed to be close to what you describe (a dictionary with dummy values). See also this related question.

For more information on time complexities refer to:

http://wiki.python.org/moin/TimeComplexity

It's not built in or included in any module I know of, but perhaps you should take a look at the Trie data structure in case you need some of its properties in the future.

Community
  • 1
  • 1
Eduardo Ivanec
  • 11,668
  • 2
  • 39
  • 42
0

Sets have O(1) membership tests on average and a nice interface.

miku
  • 181,842
  • 47
  • 306
  • 310
0

I don't know about Big-O, but here's what the Python Language References says about set types:

Common uses for sets are fast membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.

Rik Poggi
  • 28,332
  • 6
  • 65
  • 82