Good afternoon, respected experts. I am learning Python and Json.

There is such a json file:

{ "рубашка":{"kol": 32, "type": "шт"}, "брюки":{"kol": 22, "type": "шт"}, "носки":{"kol": 60, "type": "пара"}, "трусы":{"kol": 45, "type": "пара"}, "ботинки":{"kol": 21, "type": "пара"}, "кеды":{"kol": 29, "type": "пара"}, "сапоги":{"kol": 28, "type": "пара"}, "шляпа":{"kol": 20, "type": "шт"}, "кепка":{"kol": 14, "type": "шт"}, "пальто":{"kol": 15, "type": "шт"}, "футболка":{"kol": 30, "type": "шт"} } 

The decoded output is obtained:

 {'шляпа': {'kol': 20, 'type': 'шт'}, 'сапоги': {'kol': 28, 'type': 'пара'}, 'кепка': {'kol': 14, 'type': 'шт'}, 'пальто': {'kol': 15, 'type': 'шт'}, 'футболка': {'kol': 30, 'type': 'шт'}, 'брюки': {'kol': 22, 'type': 'шт'}, 'кеды': {'kol': 29, 'type': 'пара'}, 'носки': {'kol': 60, 'type': 'пара'}, 'ботинки': {'kol': 21, 'type': 'пара'}, 'трусы': {'kol': 45, 'type': 'пара'}, 'рубашка': {'kol': 32, 'type': 'шт'}} 

I decode it. And you need to count the number of piece goods and the pair, and the number of all goods. So the algorithm is not clear a little, how to check and access the sub-dictionary, that is, it will get access (and check) type: 'pc'.

  • one
    If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

By key:

 for name_product in parent_dict.keys(): print(parent_dict[name_product]['type']) 
  • Something is not working. import json myfile = json.load(open('tovar.json', 'r')) shtuka = 0 para = 0 for key in myfile.keys(): if 'шт' in key['tip']: shtuka +=1 elif 'пара' in key['tip']: para +=1 else: print('Очень сложная АШИПКА!') print('========================') print('Количество штучного товара', shtuka) print('Количество парного товара', para) print(myfile) - kelevra
  • Everything. Understood thanks! The theme worked yours. - kelevra
  • Thanks a lot, but if the answer helped, you can mark it as correct :) - Xyanight

You can use collections.Counter :

 In [232]: d Out[232]: {'ботинки': {'kol': 21, 'type': 'пара'}, 'брюки': {'kol': 22, 'type': 'шт'}, 'кеды': {'kol': 29, 'type': 'пара'}, 'кепка': {'kol': 14, 'type': 'шт'}, 'носки': {'kol': 60, 'type': 'пара'}, 'пальто': {'kol': 15, 'type': 'шт'}, 'рубашка': {'kol': 32, 'type': 'шт'}, 'сапоги': {'kol': 28, 'type': 'пара'}, 'трусы': {'kol': 45, 'type': 'пара'}, 'футболка': {'kol': 30, 'type': 'шт'}, 'шляпа': {'kol': 20, 'type': 'шт'}} In [233]: from collections import Counter In [241]: c = Counter(v['type'] for v in d.values()) In [242]: c Out[242]: Counter({'пара': 5, 'шт': 6}) In [243]: sum(c.values()) Out[243]: 11 

or, more conveniently, use the Pandas module :

 In [257]: import pandas as pd In [258]: df = pd.DataFrame(d).T In [259]: df Out[259]: kol type ботинки 21 пара брюки 22 шт кеды 29 пара кепка 14 шт носки 60 пара пальто 15 шт рубашка 32 шт сапоги 28 пара трусы 45 пара футболка 30 шт шляпа 20 шт In [260]: df.groupby('type').kol.agg(['sum','size']).reset_index() Out[260]: type sum size 0 пара 183 5 1 шт 133 6