Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested dictionary

I am working on some FASTA-like sequences (not FASTA, but something I have defined that's similar for some culled PDB from the PISCES server).

I have a question. I have a small no of sequences called nCatSeq, for which there are MULTIPLE nBasinSeq. I go through a large PDB file and I want to extract for each nCatSeq the corresponding nBasinSeq without redundancies in a dictionary. The code snippet that does this is given below.

nCatSeq=item[1][n]+item[1][n+1]+item[1][n+2]+item[1][n+3]
nBasinSeq=item[2][n]+item[2][n+1]+item[2][n+2]+item[2][n+3]
if nCatSeq not in potBasin:
    potBasin[nCatSeq]=nBasinSeq
else:   
    if nBasinSeq not in potBasin[nCatSeq]:
        potBasin[nCatSeq]=potBasin[nCatSeq],nBasinSeq
    else:
        pass

I get the following as the answer for one nCatSeq,

'4241': ((('VUVV', 'DDRV'), 'DDVG'), 'VUVV')

what I want however is :

'4241': ('VUVV', 'DDRV', 'DDVG', 'VUVV')

I don't want all the extra brackets due to the following command

potBasin[nCatSeq]=potBasin[nCatSeq],nBasinSeq 

(see above code snippet)

Is there a way to do this ?

like image 999
user1729355 Avatar asked Oct 08 '12 15:10

user1729355


People also ask

What is a nested dictionary?

Nested Dictionary: Nesting Dictionary means putting a dictionary inside another dictionary. Nesting is of great use as the kind of information we can model in programs is expanded greatly.

What is the use of nested dictionary in Python?

Here nested dictionary refers to the dictionary inside a dictionary. In simple words, it refers to the dictionary, which consists of a set of multiple dictionaries. It is used to store the data values in key-value pairs. Nesting Dictionary means putting a dictionary inside another dictionary.

How do I get nested dictionary?

To create a nested dictionary, simply pass dictionary key:value pair as keyword arguments to dict() Constructor. You can use dict() function along with the zip() function, to combine separate lists of keys and values obtained dynamically at runtime.

Is nested dictionary possible?

Nested dictionary is an unordered collection of dictionary. Slicing Nested Dictionary is not possible. We can shrink or grow nested dictionary as need. Like Dictionary, it also has key and value.


2 Answers

The problem is putting a comma to "append" an element just creates a new tuple every time. To solve this you use lists and append:

nCatSeq=item[1][n]+item[1][n+1]+item[1][n+2]+item[1][n+3]
nBasinSeq=item[2][n]+item[2][n+1]+item[2][n+2]+item[2][n+3]
if nCatSeq not in potBasin:
    potBasin[nCatSeq]=[nBasinSeq]
elif nBasinSeq not in potBasin[nCatSeq]:
        potBasin[nCatSeq].append(nBasinSeq)

Even better would be to instead of making potBasin a normal dictionary, replace it with a defaultdict. The code can then be simplified to:

# init stuff
from collections import defaultdict
potBasin = defaultdict(list)

# inside loop
nCatSeq=item[1][n]+item[1][n+1]+item[1][n+2]+item[1][n+3]
nBasinSeq=item[2][n]+item[2][n+1]+item[2][n+2]+item[2][n+3]
potBasin[nCatSeq].append(nBasinSeq)
like image 158
Rob Wouters Avatar answered Nov 04 '22 05:11

Rob Wouters


You can add them as tuples:

if nCatSeq not in potBasin:
    potBasin[nCatSeq] = (nBasinSeq,)
else:
    if nBasinSeq not in potBasin[nCatSeq]:
        potBasin[nCatSeq] = potBasin[nCatSeq] + (nBasinSeq,)

That way, rather than:

(('VUVV', 'DDRV'), 'DDVG')
# you will get
('VUVV', 'DDRV', 'DDVG') # == ('VUVV', 'DDRV')+ ('DDVG',)
like image 39
Andy Hayden Avatar answered Nov 04 '22 07:11

Andy Hayden