def c_dict():
"""
[
{
"type": "折扣",
"word": "9折"
},
{
"type": "折扣",
"word": "满两件8折"
},
{
"type": "折扣",
"word": "满5件5折"
},
{
"type": "买赠",
"word": "买1增1"
},
{
"type": "满减",
"word": "满100减10"
},
{
"type": "满减",
"word": "满200减30"
}
]
转化成
[
{
"type": "折扣",
"words": [
"9折",
"满两件8折",
"满5件5折"
],
"number": 3
},
{
"type": "买赠",
"words": [
"买1增1"
],
"number": 1
},
{
"type": "满减",
"words": [
"满100减10",
"满200减30"
],
"number": 2
}
]
"""
tmp_site_type = [
{'type': '折扣', 'word': '9折'},
{'type': '折扣', 'word': '满两件8折'},
{'type': '折扣', 'word': '满5件5折'},
{'type': '买赠', 'word': '买1增1'},
{'type': '满减', 'word': '满100减10'},
{'type': '满减', 'word': '满200减30'}
]
tmp_dict = dict()
for l in tmp_site_type:
if l.get("type") in tmp_dict:
i += 1
tmp_dict[l.get("type")]["words"].append(l.get("word"))
tmp_dict[l.get("type")]["count"] = i
else:
i = 1
tmp = {"words": [l.get("word")]}
tmp_dict[l.get("type")] = tmp
tmp_list = list()
for key, value in tmp_dict.items():
tmp_list.append({
'type': key,
'words': value.get('words'),
'number': len(value.get('words'))
})
import json
print(json.dumps(tmp_site_type, indent=4, ensure_ascii=False))
print(json.dumps(tmp_dict, indent=4, ensure_ascii=False))
print(json.dumps(tmp_list, indent=4, ensure_ascii=False))
参考:https://www.jianshu.com/p/70dac7574491
网友评论