extending dictonary
InvisibleRoads Patrol
patrol at invisibleroads.com
Fri Dec 11 09:58:28 EST 2009
On Sat, 21 Nov 2009 09:25:38 +0100, nospam <"knutjbj(nospam)"@online.no>
wrote:
> Is there any way to extend the dictonary in such manner that I can
> insert muliplay value to each keys and return one of the value as the
> default value. I would like to have similar syste that I drawed out
below.
>
>
> tree[nucelotide_postionc][nucleotide]=default(value subtree) This should
> be returned when doing lookup without any
> tree[nucelotide_postionc][nucleotide]=Postive value
> tree[nucelotide_postionc][nucleotide]=Negative value
You could use the collections.defaultdict method.
>>> >>> import collections
>>> >>> example1 = collections.defaultdict(int)
>>> >>> example1[1]
0
>>> >>> example2 = collections.defaultdict(list)
>>> >>> example2[1]
[]
>>> >>> example3 = collections.defaultdict(dict)
>>> >>> example3[1]
{}
>>> >>> # Make nested default dictionary
>>> >>> def getNewOuterDictionary():
... return collections.defaultdict(getNewInnerDictionary)
>>> >>> def getNewInnerDictionary():
... return collections.defaultdict([])
>>> >>> example4[1][1]
[]
Hope that helps.
--
Roy Hyunjin Han
http://invisibleroads.com
We train people to become software developers and connect them to jobs and
projects for local businesses.
More information about the Python-list
mailing list