Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

extending dictonary

1 view
Skip to first unread message

nospam

unread,
Nov 21, 2009, 3:25:38 AM11/21/09
to
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

Steven D'Aprano

unread,
Nov 21, 2009, 4:50:28 AM11/21/09
to

I'm sorry, I don't understand what you are asking. What are tree,
nucelotide_postionc, nucleotide, subtree? What are positive value and
negative value?

If each key has multiple values, how do you want to specify which value
to return?


The simplest way to associate multiple values with a key is to use a list:

>>> d = {}
>>> d['a'] = [2, 3, 4]
>>> d['b'] = [5, 6]
>>> d['a'].append(0)
>>> d
{'a': [2, 3, 4, 0], 'b': [5, 6]}

If you like, you can take the convention that the first item in the list
is the default, and write this:

>>> print d['b'][0]
5


--
Steven

InvisibleRoads Patrol

unread,
Dec 11, 2009, 9:47:13 AM12/11/09
to pytho...@python.org
On Sat, 21 Nov 2009 09:25:38 +0100, nospam <"knutjbj(nospam)"@online.no>
wrote:


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
Training people to become software developers and connecting them to jobs
and projects for local businesses

InvisibleRoads Patrol

unread,
Dec 11, 2009, 9:58:28 AM12/11/09
to knu...@online.no, pytho...@python.org
On Sat, 21 Nov 2009 09:25:38 +0100, nospam <"knutjbj(nospam)"@online.no>
wrote:

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.

0 new messages