I am using Python.
Adding an integer to dictionary value in dictionary adds to all dictionaries in the dictionary.
def addQty(req_data, Cat):
for item in req_data:
state = item.get("state")
sub_cat = item.get("sub_category")
qty = item.get("quantity")
if state in Cat:
Cat[state][sub_cat] += int(qty)
print(Cat)
The code adds to all dictionaries in the dictionary.
Dictionary Cat
is created using:
def state_and_subcat(State, Subcat):
Cat = State.copy()
for state in StateSubCat:
Cat[state] = Subcat
return Cat
where State
is built with {"state": state, "sub_category": subcat, "quantity": qty}
Using a deepcopy instead of .copy() does not seem to solve the problem.
An example of Cat
is
{ "Alabama": { "Accessories": 2976, "Appliances": 1729, "Art": 3000, "Binders": 5974, "Bookcases": 868, "Chairs": 2356, "Copiers": 234, "Envelopes": 906, "Fasteners": 914, "Furnishings": 3563, "Labels": 1400, "Machines": 440, "Paper": 5178, "Phones": 3289, "Storage": 3158, "Supplies": 647, "Tables": 1241 }, "Arizona": { "Accessories": 2976, "Appliances": 1729, "Art": 3000, "Binders": 5974, "Bookcases": 868, "Chairs": 2356, "Copiers": 234, "Envelopes": 906, "Fasteners": 914, "Furnishings": 3563, "Labels": 1400, "Machines": 440, "Paper": 5178, "Phones": 3289, "Storage": 3158, "Supplies": 647, "Tables": 1241 }, "Arkansas": { "Accessories": 2976, "Appliances": 1729, "Art": 3000, "Binders": 5974, "Bookcases": 868, "Chairs": 2356, "Copiers": 234, "Envelopes": 906, "Fasteners": 914, "Furnishings": 3563, "Labels": 1400, "Machines": 440, "Paper": 5178, "Phones": 3289, "Storage": 3158, "Supplies": 647, "Tables": 1241 }, "California": { "Accessories": 2976, "Appliances": 1729, "Art": 3000, "Binders": 5974, "Bookcases": 868, "Chairs": 2356, "Copiers": 234, "Envelopes": 906, "Fasteners": 914, "Furnishings": 3563, "Labels": 1400, "Machines": 440, "Paper": 5178, "Phones": 3289, "Storage": 3158, "Supplies": 647, "Tables": 1241 }
An example of req_data
is:
{ "quantity": "7", "state": "Arizona", "sub_category": "Storage" }, { "quantity": "2", "state": "California", "sub_category": "Chairs" }, { "quantity": "3", "state": "California", "sub_category": "Furnishings" }, { "quantity": "5", "state": "Ohio", "sub_category": "Phones" }, { "quantity": "2", "state": "Ohio", "sub_category": "Paper" }, { "quantity": "9", "state": "Ohio", "sub_category": "Fasteners" }, { "quantity": "3", "state": "California", "sub_category": "Paper" }, { "quantity": "1", "state": "California", "sub_category": "Paper" }, { "quantity": "4", "state": "California", "sub_category": "Art" }, { "quantity": "2", "state": "California", "sub_category": "Fasteners" }, { "quantity": "3", "state": "California", "sub_category": "Binders" }, { "quantity": "6", "state": "California", "sub_category": "Supplies" }, { "quantity": "14", "state": "California", "sub_category": "Furnishings" }, { "quantity": "2", "state": "Pennsylvania", "sub_category": "Furnishings" }, { "quantity": "3", "state": "California", "sub_category": "Art" }
How can I add to the specific qty
to the sub_cat
in the state
only and not to all state
s?