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))
python 统计列表中相同key的字典
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 大家好,我是 bug,一个普通程序员。 程序员的快速成长,在于多练习,多输出,多分享,多链接。怎奈执行力太差,错过...
- 手记 -- encoding=utf-8 -- python3代码 import operator一. 按字典值排...
- 一、sorted高阶函数 例子: 下面是sorted排序方法的详细解释: sorted高阶函数语法格式: sor...