KeyError when building a multidimensional dictionary in Python

I am trying to create a dictionary with two keys, but I get a KeyError when assigning elements. I do not get an error when using each of the keys separately, and the syntax seems pretty simple, so I'm at a dead end.

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'] 
                                }

I do not believe that the problem is with feedparser (which converts xml to dict) or with the results that I get with amazon, since I have no problem creating a dict when using 'allProducts [x]' or 'allProducts [index]' but not both.

What am I missing?

+3
source share
5 answers

allProducts[index][x], allProducts[index] dict, x dict.

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
+5

Python 2.5 , collections.defaultdict.

:

allProducts = {}

:

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 {}
+3

setdefault .

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

python, dict dict. , 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'] 
                                }
0

Source: https://habr.com/ru/post/1790556/


All Articles