嵌套字典 [英] Nested dictionary
问题描述
我正在处理一些类似FASTA的序列(不是FASTA,但是我已经定义了与PISCES服务器的一些被剔除的PDB相似的东西)。
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).
我有一个问题。我有一个名为 nCatSeq
的序列的小号,其中有MULTIPLE nBasinSeq
。我经历了一个大型的PDB文件,我想提取每个 nCatSeq
相应的 nBasinSeq
而不会在字典中有冗余。下面给出了代码片段。
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
我得到以下内容作为一个nCatSeq的答案,
I get the following as the answer for one nCatSeq,
'4241': ((('VUVV', 'DDRV'), 'DDVG'), 'VUVV')
我想要的是:
'4241':('VUVV','DDRV','DDVG','VUVV' p>
'4241': ('VUVV', 'DDRV', 'DDVG', 'VUVV')
由于以下命令,我不想要所有的额外括号
I don't want all the extra brackets due to the following command
potBasin[nCatSeq]=potBasin[nCatSeq],nBasinSeq
见上面的代码片段)
有没有办法这样做?
推荐答案
您可以将它们添加为元组:
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,)
这样,而不是:
(('VUVV', 'DDRV'), 'DDVG')
# you will get
('VUVV', 'DDRV', 'DDVG') # == ('VUVV', 'DDRV')+ ('DDVG',)
这篇关于嵌套字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!