2017-08-16 5 views
-5

は、以下のようなものです:マルチレベル辞書構造を定義し、それにデータを挿入する方法は?私は辞書の<strong>defaultdict</strong>【選択構造を用いたマルチレベルの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]) 

しかし、何らかのエラーが発生しています。だから誰も私はマルチレベル辞書を作成するのに役立つことができますか?

+0

あなたは壁にぶつかっていることを認識するために何回頭を使って壁を打つ必要がありますか? – alfasin

+0

@alfasin - できませんか? – Arijit

+0

もちろん、それは可能ですが、実際には、dct ['source1'] ['gene'] ['gene1'] ['mrna'] ['1'] ['CDS' ] ['location']。append([100、200]) '? – alfasin

答えて

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' 
+0

助けてくれてありがとう。しかし、上記の辞書は** list **を保持することはできません。それで私を助けてください。 – Arijit

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"のような何かが間違っていた場所を実際に見るからです。

1

あなたの辞書の定義は、あなたが追加したいデータ型以外のものを使用しています - (スペルミスのためにChristianFigueroas answereをチェックしてください)。

コードを実行すると、エラーAttributeError: 'collections.defaultdict' object has no attribute 'append'が発生します。これが、正しいデータ型を持つ辞書を作成した理由です(既定の辞書の代わりに辞書を使用することを怠惰に思う)。

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]) 

私はあなたが何をしたのかを見てほしい。

関連する問題