简体   繁体   中英

Nested dict key overwritten

I want to create a nested dict through a for loop. All the customers and categories keys are created as expected (i and j), but for the product_categories only the last key is written. I know it's probably getting overwritten, but I can't understand why?

top_10_recs = {}

for i, j, k in itertools.product(customers, categories, product_categories):
    if i not in top_10_recs:
        top_10_recs[i] = {}
    if j not in top_10_recs:
        top_10_recs[i][j] = {}
    if k not in top_10_recs:
        top_10_recs[i][j][k] = {}
    try:
        top_10_recs[i][j][k] = trained_dataframe.loc[(i, j), k].nlargest(10).to_dict()
    except:
        pass

If I do a print with this:

for i, j, k in itertools.product(customers, categories, product_categories):
    try:
        print("{}".format(k))
        print(trained_dataframe.loc[(i, j), k].nlargest(10))

I get the expected result, looping through all product_categories.

You need to be careful to only create new keys inside dictionaries if they do not already exist in those dictionaries. The snippet below should work.

if i not in top_10_recs:
    top_10_recs[i] = {}
if j not in top_10_recs[i]:
    top_10_recs[i][j] = {}
if k not in top_10_recs[i][j]:
    top_10_recs[i][j][k] = {}

Consider also using except KeyError instead of just except , which catches all possible errors (you do not want that).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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