如何定义多级字典结构并将数据插入其中?

时间:2017-08-16 05:30:20

标签: python dictionary

我正在尝试使用 defaultdict 创建一个多级Python字典。字典的结构如下所示:

{
    "source1": {
        "gene": {
            "gene1": {
                "location": [
                    [
                        10,
                        200
                    ]
                ],
                "mrna": {
                    "1": {
                        "location": [
                            [
                                10,
                                200
                            ]
                        ],
                        "product": "hypothetical",
                        "CDS": {
                            "location": [
                                [
                                    10,
                                    50
                                ],
                                [
                                    100,
                                    200
                                ]
                            ]
                        }
                    }
                }
            }
        }
    }
}

但在Python的这种情况下,我们需要在插入任何数据之前定义结构。

我尝试定义结构是:

from collections import defaultdict

dct = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(
        lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(list)))))))

现在要在上面的结构中插入数据,我使用下面的代码来创建上面定义的格式。

dct['source1']['gene']['gene1']['location'].append([10, 200])
dct['source1']['gene']['gene1']['mrna']['1']['location'].append(['10', '200'])
dct['source1']['gene']['gene1']['mrna']['1']['product'] = 'hypothetical' 
dct['source1']['gene']['gene1']['mrna']['1']['CDS']['location'].append([10, 50])
dct['source1']['gene']['gene1']['mrna']['1']['CDS']['location'].append([100, 200])

但是我收到了一些错误。那么任何人都可以帮我创建多级字典吗?

3 个答案:

答案 0 :(得分:1)

您的字典定义使用的是除您要添加的数据类型之外的其他数据类型 -  (还要检查ChristianFigueroas是否因为你的拼写问题而回答。)

如果我运行您的代码,我会收到错误AttributeError: 'collections.defaultdict' object has no attribute 'append'。这就是为什么我用正确的数据类型创建你的字典(借口懒惰我使用字典而不是默认的dicts)。

dct = {}                                                    #Dict
dct['source1'] = {}                                         #Dict
dct['source1']['gene'] = {}                                 #Dict
dct['source1']['gene']['gene1'] = {}                        #Dict
dct['source1']['gene']['gene1']['location'] = []            #List
dct['source1']['gene']['gene1']['mrna'] = {}                #Dict
dct['source1']['gene']['gene1']['mrna']['1'] = {}           #Dict
dct['source1']['gene']['gene1']['mrna']['1']['location'] = [] #List
dct['source1']['gene']['gene1']['mrna']['1']['product'] = ''  #String
dct['source1']['gene']['gene1']['mrna']['1']['CDS'] = {}    #Dict
dct['source1']['gene']['gene1']['mrna']['1']['CDS']['location'] = [] #List


dct['source1']['gene']['gene1']['location'].append([10, 200])
dct['source1']['gene']['gene1']['mrna']['1']['location'].append(['10', '200'])
dct['source1']['gene']['gene1']['mrna']['1']['product'] = 'hypothetical'
dct['source1']['gene']['gene1']['mrna']['1']['CDS']['location'].append([10, 50])
dct['source1']['gene']['gene1']['mrna']['1']['CDS']['location'].append([100, 200])

我希望你看到我在那里做了什么。

答案 1 :(得分:0)

subdicts的建立可以自动完成:

>>> from collections import defaultdict
>>> f = lambda: defaultdict(f)
>>> d = f()
>>> d['usa']['texas'] = 'lone star'
>>> d['usa']['ohio'] = 'buckeye'
>>> d['canada']['alberta'] = 'flames'

答案 2 :(得分:0)

在您的代码中,您尝试获取dct["source1"]["gene"]["gene1"]["mrna"]["1"]["CDS"]["location"],但没有"CDS"密钥,只有"cds"。将"CDS"替换为"cds"

dct["source1"]["gene"]["gene1"]["mrna"]["1"]["cds"]["location"].append( ... )

Python键区分大小写,因此请确保您完全匹配字符串

此外,我建议不要将您的数据放入超级特定的dict中,因为这样会更难调试并实际查看出现问题的地方,例如区分大小写的"CDS"