简体   繁体   English

嵌套字典 python 语法

[英]Nested dictionary python syntax

Running into some syntax problems trying to generate a nested dictionary.尝试生成嵌套字典时遇到一些语法问题。 My code attempt is the following:我的代码尝试如下:

from collections import defaultdict

count = 0
target_dict = defaultdict(dict)
for alignment in item.alignments:
          for hsp in alignment.hsps:
                 if hsp.expect < E_VALUE_THRESH:
                    try:
                        target_dict['Entry'+'_'+str(count)]['Sequence'+'_'+str(count)] = alignment.title,/
                                                           ['Length'+('_'+str(count))] = alignment.length,/
                                                           ['Score'+('_'+str(count))] == hsp.score/
                                                           ['Hsp_query'+('_'+str(count))] == ("{}".format(hsp.query[0:90]))/
                                                           ['Hsp_match'+('_'+str(count))] == ("{}".format(hsp.match[0:90]))/
                                                           ['Hsp_sbjct'+('_'+str(count))] == ("{}".format(hsp.sbjct[0:90]))



                    except KeyError as e:
                        print('exception missing key', e)
                    count+=1

Gives kindof what I'm looking for (but when trying to add additional key:value pairs inside the entry key I get syntax errors:给出了我正在寻找的东西(但是当试图在入口键中添加额外的键:值对时,我得到语法错误:

 target_dict['Entry'+'_'+str(count)]['Sequence'+'_'+str(count)] = alignment.title,/
                                                                                     ^
SyntaxError: invalid syntax

Desired output is:所需的 output 是:

{'Entry_0': {'Sequence_0': 'gi|1219041180|ref|XM_021875076.1| PREDICTED: Chenopodium quinoa cold-regulated 413 plasma membrane, 
 'Length_0': 1173,
 'Score_0': 482.0,
 'Hsp_query_0': 'ACAGAAAATGGGGAGAGAAATGAAGTACTTGGCCATGAAAACTGATCAATTGGCCGTGGCTAATATGATCGATTCCGATATCAATGAGCT',
 'Hsp_match_0': '|| ||||||||| |||| | |||| ||  |||| |||| | |||| ||| | |||| ||| ||| ||||| | ||||| |||||||||||',
 'Hsp_sbjct_0': 'ACCGAAAATGGGCAGAGGAGTGAATTATATGGCAATGACACCTGAGCAACTAGCCGCGGCCAATTTGATCAACTCCGACATCAATGAGCT',

How can I make this nested dictionary?我怎样才能制作这个嵌套字典? Any help on how I'm going wrong with syntax would be much appreciated thank you!任何有关我如何在语法上出错的帮助将不胜感激,谢谢!

I think you are trying to "add to the [inner] dictionary" incorrectly.我认为您正在尝试错误地“添加到 [inner] 字典”。 This is a toy example:这是一个玩具示例:

from collections import defaultdict

# fake data
titles = ['dog', 'cat']
colors = ['brown', 'yellow']
weight = [20, 2]

entries = defaultdict(dict)

for idx, title in enumerate(titles):
    # make the new inner dictionary with one key:value pair, color
    entries[title]['color'] = colors[idx]

    # add to inner dictionary with second key:value pair
    entries[title]['weight'] = weight[idx]

print(entries['cat'])

However, I'm not sure why you are using a nested dictionary when the keys to your outer dictionary are just Entry_0 and Entry_1 .... Why don't you just make a list of dictionaries with the schema that the list index is the entry number?但是,当外部字典的键只是Entry_0Entry_1时,我不确定为什么要使用嵌套字典 .... 为什么不使用列表索引的架构制作字典列表入境号码? You could then create dictionaries and append them to the list easily.然后,您可以轻松地创建字典和 append 到列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM