Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double-indexed dictionary

I want to be able to store and look up values in a dictionary based on two integer values.

So when I look up a value I want to use the keys read_length and min_size to access the element, like so:

number_of_read_lengths[read_length][min_size]

I know I can create nested dictionaries, but that is a slight hassle.

Is there a simple way of doing what I want to do?

like image 306
The Unfun Cat Avatar asked Oct 25 '12 09:10

The Unfun Cat


People also ask

Can 2 values have the same key in dictionary?

In python, if we want a dictionary in which one key has multiple values, then we need to associate an object with each key as value. This value object should be capable of having various values inside it. We can either use a tuple or a list as a value in the dictionary to associate multiple values with a key.

Can a dictionary be indexed?

Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys.

How do you create a double dictionary in Python?

You can create a nested dictionary in Python by placing comma-separated dictionaries within curly braces {}. A Python nested dictionary allows you to store and access data using the key-value mapping structure within an existing dictionary.

Can dictionary have multiple data types?

One can only put one type of object into a dictionary. If one wants to put a variety of types of data into the same dictionary, e.g. for configuration information or other common data stores, the superclass of all possible held data types must be used to define the dictionary.


4 Answers

You can use any immutable and hashable object as key, including tuples

number_of_read_lengths = {}  number_of_read_lengths[14,3] = "Your value" 
like image 112
Johanna Larsson Avatar answered Nov 02 '22 19:11

Johanna Larsson


Using tuples could be quite annoying -- you got to remember to place the tuple during indexing.

I would recommend a nested dict, but a defaultdict, like so:

from collections import defaultdict  number_of_read_lengths = defaultdict(dict)  number_of_read_lengths[1][2] = 3  print(number_of_read_lengths) 

This code would give:

defaultdict(<type 'dict'>, {1: {2: 3}}) 

This way, any non-existing element in the number_of_read_lengths dict will be created as a dict when accessing or setting it. Simple and effective.

More info on defaultdict: http://docs.python.org/library/collections.html#collections.defaultdict There are also examples: http://docs.python.org/library/collections.html#defaultdict-examples

like image 45
pawroman Avatar answered Nov 02 '22 19:11

pawroman


You could try to use tuples as keys:

number_of_read_lengths[(read_length, min_size)]
like image 27
Pierre GM Avatar answered Nov 02 '22 17:11

Pierre GM


Just to expand a bit more from the comment I made:

A dict key must be hashable, which a simple tuple is. However, a tuple that contains unhashable values such as lists, is not hashable (even though it is immutable!) and therefore cannot be used as dict key:

>>> bad = ([12],[32])
# still immutable
>>> bad[1] = [21]
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

# but not hashable!
>>> d = {}
>>> d[bad] = 2
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unhashable type: 'list'

You can even have mutable and hashable objects as dict keys, but it's not really useful and should be avoided.

like image 42
K Z Avatar answered Nov 02 '22 19:11

K Z