Nested dict key overwritten
up vote
0
down vote
favorite
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.
python
add a comment |
up vote
0
down vote
favorite
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.
python
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
python
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.
python
python
asked yesterday
joddm
1661110
1661110
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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).
New contributor
Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
– joddm
yesterday
1
I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
– Patol75
yesterday
Yes, I want to have all the objects in categories as keys in the dict
– joddm
yesterday
@joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
– Patol75
yesterday
It works :) Thanks for helping me out!
– joddm
yesterday
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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).
New contributor
Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
– joddm
yesterday
1
I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
– Patol75
yesterday
Yes, I want to have all the objects in categories as keys in the dict
– joddm
yesterday
@joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
– Patol75
yesterday
It works :) Thanks for helping me out!
– joddm
yesterday
|
show 1 more comment
up vote
1
down vote
accepted
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).
New contributor
Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
– joddm
yesterday
1
I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
– Patol75
yesterday
Yes, I want to have all the objects in categories as keys in the dict
– joddm
yesterday
@joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
– Patol75
yesterday
It works :) Thanks for helping me out!
– joddm
yesterday
|
show 1 more comment
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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).
New contributor
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).
New contributor
edited yesterday
New contributor
answered yesterday
Patol75
5216
5216
New contributor
New contributor
Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
– joddm
yesterday
1
I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
– Patol75
yesterday
Yes, I want to have all the objects in categories as keys in the dict
– joddm
yesterday
@joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
– Patol75
yesterday
It works :) Thanks for helping me out!
– joddm
yesterday
|
show 1 more comment
Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
– joddm
yesterday
1
I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
– Patol75
yesterday
Yes, I want to have all the objects in categories as keys in the dict
– joddm
yesterday
@joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
– Patol75
yesterday
It works :) Thanks for helping me out!
– joddm
yesterday
Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
– joddm
yesterday
Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
– joddm
yesterday
1
1
I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
– Patol75
yesterday
I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
– Patol75
yesterday
Yes, I want to have all the objects in categories as keys in the dict
– joddm
yesterday
Yes, I want to have all the objects in categories as keys in the dict
– joddm
yesterday
@joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
– Patol75
yesterday
@joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
– Patol75
yesterday
It works :) Thanks for helping me out!
– joddm
yesterday
It works :) Thanks for helping me out!
– joddm
yesterday
|
show 1 more comment
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371969%2fnested-dict-key-overwritten%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown