Home > Mobile >  Nesting dictionary of dictionaries
Nesting dictionary of dictionaries

Time:11-07

I am trying to nest two dictionaries, perhaps it's easier to just make one dictionary of dictionaries, however, I have the same keys with different values, would it be easier to just change the keys or is there a way to nest these two?

Number_As = 1

#DNA dictionary
DNAdict = {
  'A': ['dAMP',1,..., other values],
  'C': ['dCMP',2,..., other values],
  'T': ['dTMP',3,..., other values],
  'G': ['dGMP',4,..., other values]}

#RNA dictionary
RNAdict = {
  'A': ['AMP',5,..., other values],
  'C': ['CMP',6,..., other values],
  'U': ['UMP',7,..., other values],
  'G': ['GMP',8,..., other values]}

I am then trying to access individual values for different calculations:

Energy of DNA_A = Number_As * DNAdict['A'][1]

Energy of DNA_A = 1

or 

Energy of RNA_A = Number_As * RNAdict['A'][1]

Energy of RNA_A = 5

But I would like to have only one dictionary and then have access to the different values of each key despite the same name. Is it possible?

Thank you lots!!

CodePudding user response:

yes, all the possiblities, its possible

1: Hard code:

grouped = {
'DNAdict':{
  'A': ['dAMP',1,..., ],
  'C': ['dCMP',2,..., ],
  'T': ['dTMP',3,..., ],
  'G': ['dGMP',4,..., ]},
'RNAdict':{
  'A': ['AMP',5,..., ],
  'C': ['CMP',6,..., ],
  'U': ['UMP',7,..., ],
  'G': ['GMP',8,..., ]}
}

#acessing RNAdict a[0]

value = grouped['RNAdict']['A'][0]
print(value)

2: grouped in code

#RNA dictionary
DNAdict = {
  'A': ['dAMP',1,..., ],
  'C': ['dCMP',2,..., ],
  'T': ['dTMP',3,..., ],
  'G': ['dGMP',4,..., ]}

#RNA dictionary
RNAdict = {
  'A': ['AMP',5,..., ],
  'C': ['CMP',6,..., ],
  'U': ['UMP',7,..., ],
  'G': ['GMP',8,..., ]}

grouped = {'DNAdict':DNAdict,'RNAdict':RNAdict}

#example of finding
value = grouped['RNAdict']['A'][0]
print(value)

3 Adding Prefix:


def add_prefix(elem:dict,prefix:str)->dict:
    new_dict = {}
    for key in elem.keys():
        new_dict[f'{prefix}_{key}'] = elem[key]
    return new_dict


DNAdict = {
  'A': ['dAMP',1,..., ],
  'C': ['dCMP',2,..., ],
  'T': ['dTMP',3,..., ],
  'G': ['dGMP',4,..., ]}

#RNA dictionary
RNAdict = {
  'A': ['AMP',5,..., ],
  'C': ['CMP',6,..., ],
  'U': ['UMP',7,..., ],
  'G': ['GMP',8,..., ]}

Dna = add_prefix(elem=DNAdict,prefix='DNA')
Rna = add_prefix(elem=RNAdict,prefix='RNA')
Nested= {**Dna, **Rna}

CodePudding user response:

How do you know which dict do you want to use right now? If you want to keep little control over the individual elements of your dict in its current state (e.g., ['dAMP',1,..., other values], you can make the value of the dict a list of lists and refer to a desired element by an index. However, if you can assign each element a unique name, indeed you can create a nested dict with the following code:

# Your current code goes here

acids = {'DNA': RNAdict, 'DNA': DNAdict}

# Then you can access a specific dict by
dna_a_energy = Number_As * acids['DNA']['A'][1]
rna_a_energy = Number_As * acids['RNA']['A'][1]

If I understood you correctrly.

  • Related