2011-02-07 5 views
3

2つのキーを持つ辞書を作成しようとしていますが、アイテムを割り当てるときにKeyErrorが発生しています。私は、それぞれのキーを別々に使用するときにエラーが出ることはありません。構文はかなり単純なので、私は困惑しています。 「allProducts [Xのいずれかを使用したときに、私は何の問題は辞書を構築していないとして、私は、(辞書にXMLを変換する)問題がfeedparserであると考えているか、私はアマゾンから取得していた結果ではありません多次元辞書をPythonで構築中のKeyError

searchIndices = ['Books', 'DVD'] 
allProducts = {} 
for index in searchIndices: 
    res = amazon.ItemSearch(Keywords = entity, SearchIndex = index, ResponseGroup = 'Large', ItemPage = 1, Sort = "salesrank", Version = '2010-11-01') 
    products = feedparser.parse(res) 
    for x in range(10): 
     allProducts[index][x] = { 'price' : products['entries'][x]['formattedprice'], 
            'url' : products['entries'][x]['detailpageurl'], 
            'title' : products['entries'][x]['title'], 
            'img' : products['entries'][x]['href'], 
            'rank' : products['entries'][x]['salesrank'] 
           } 

] 'または' allProducts [index] 'ですが、両方ではありません。

私には何が欠けていますか?

答えて

5

allProducts[index][x]に割り当てるためには、最初のルックアップを取得するためにallProducts[index]上で行われますdictの場合、割り当てられている値はそのdictのインデックスxに格納されます。

ただし、ループを初めて実行すると、allProducts[index]はまだ存在しません。これを試してみてください:

for x in range(10): 
    if index not in allProducts: 
     allProducts[index] = { } # or dict() if you prefer 
    allProducts[index][x] = ... 

事前にallProductsにすることになっているすべてのインデックスを知っているので、あなたの代わりに、このように手の前に、それを初期化することができます:あなたが使用することができます

map(lambda i: allProducts[i] = { }, searchIndices) 
for index in searchIndices: 
    # ... rest of loop does not need to be modified 
+0

美しいが、私は、Pythonに新たなんだと忘れて、次で

allProducts = {} 

単にラインを置き換えますそれは自動化されません。ありがとう! – kasceled

+0

@tippytop:ありがとう、助けてくれてうれしいです(投票数の下にあるチェックマークの枠をクリックして、私の答えを受け入れたものとしてマークすることができます)。 Pythonの初心者だから(あなたはPerlのプログラマーのように思える;-)) 'range'と' xrange'の違いを知らないかもしれない - 'range'はすべての数字のリストをメモリ内に作成し、 'xrange'は、数字を1つずつ生成するイテレータを作成します。この場合、 'range'は小さなリストしか作成していないのでOKですが、一般的に' xrange'が好ましいです。 – Cameron

0

あなたはdict内のdictだとpythonに伝える必要があります。 allProducts [index]は別の辞書でなければならないということは知られていません。

あなたはこれを行う(またはdefaultdictを使用)しようとしているときには、このような行を追加する必要があり

allProducts = {} 
for index in searchIndices: 
    allProducts[index] = {} 
0
searchIndices = ['Books', 'DVD'] 
allProducts = {} 
for index in searchIndices: 
    res = amazon.ItemSearch(Keywords = entity, SearchIndex = index, ResponseGroup = 'Large', ItemPage = 1, Sort = "salesrank", Version = '2010-11-01') 
    products = feedparser.parse(res) 
    for x in range(10): 
     if not allProducts.has_key(index): 
      allProducts[index] = {} 
     allProducts[index][x] = { 'price' : products['entries'][x]['formattedprice'], 
            'url' : products['entries'][x]['detailpageurl'], 
            'title' : products['entries'][x]['title'], 
            'img' : products['entries'][x]['href'], 
            'rank' : products['entries'][x]['salesrank'] 
           } 
1

setdefault辞書の方法。

for x in range(10): 
     allProducts.setdefault(index, {})[x] = ... 
3

あなたは、Python 2.5以降を使用している場合は、このような状況はcollections.defaultdictのためにオーダーメイドです。

import collections 
allProducts = collections.defaultdict(dict) 

使用されている。この例

>>> import collections 
>>> searchIndices = ['Books', 'DVD'] 
>>> allProducts = collections.defaultdict(dict) 
>>> for idx in searchIndices: 
... print idx, allProducts[idx] 
... 
Books {} 
DVD {} 
関連する問題