python – Double-indexed dictionary

python – Double-indexed dictionary

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

number_of_read_lengths = {}

number_of_read_lengths[14,3] = Your value

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

python – Double-indexed dictionary

You could try to use tuples as keys:

number_of_read_lengths[(read_length, min_size)]

Leave a Reply

Your email address will not be published. Required fields are marked *