Last active
February 4, 2020 08:50
-
-
Save skar404/e7d6dd6a3f88db7c2879a1a3d933b347 to your computer and use it in GitHub Desktop.
group by
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from base64 import b64encode | |
from json import dumps, loads, JSONEncoder | |
import pickle | |
class AllTypeJSONEncoder(JSONEncoder): | |
def default(self, obj): | |
if isinstance(obj, (list, dict, str, int, float, bool, type(None))): | |
return super().default(obj) | |
elif isinstance(obj, (set, )): | |
return list(obj) | |
return {'_python_object': b64encode(pickle.dumps(obj)).decode('utf-8')} | |
input_json = loads("""[ { | |
"shop" : "Яндекс.Еда", | |
"product" : "Манты" | |
}, { | |
"shop" : "Яндекс.Еда", | |
"product" : "Смузи" | |
}, { | |
"shop" : "Яндекс.Еда", | |
"product" : "Сырные палочки" | |
}, { | |
"shop" : "Яндекс.Еда", | |
"product" : "Шашлык" | |
}, { | |
"shop" : "Яндекс.Музей", | |
"product" : "Футболка протирашка" | |
}, { | |
"shop" : "Яндекс.Маркет", | |
"product" : "Электробритва" | |
}, { | |
"shop" : "Яндекс.Еда", | |
"product" : "Детское пюре" | |
}, { | |
"shop" : "Яндекс.Еда", | |
"product" : "Роллы" | |
}, { | |
"shop" : "Яндекс.Маркет", | |
"product" : "Ноутбук" | |
}, { | |
"shop" : "Яндекс.Музей", | |
"product" : "Фонарь Яндекс" | |
}, { | |
"shop" : "Яндекс.Маркет", | |
"product" : "Телевизор" | |
}, { | |
"shop" : "Яндекс.Маркет", | |
"product" : "Планшет" | |
}, { | |
"shop" : "Яндекс.Еда", | |
"product" : "Тирамису" | |
} ]""") | |
group_by_dict = {} | |
for row in input_json: | |
shop = row['shop'] | |
product = row['product'] | |
if shop not in group_by_dict: | |
# set - это как list, но хранит только уникальные значения | |
group_by_dict[shop] = set() | |
group_by_dict[shop].add(product) | |
print(dumps(group_by_dict, ensure_ascii=False, cls=AllTypeJSONEncoder)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment