字典输出生成如何进行改名

1、我是初学者,自学点点滴滴,目前遇到个问题,
mongodb数据库有这些的数据,其中huifuzhuagntai的0是未回复的意思,1是已回复的意思
{id:1,huifuzhuangtai:0,title:标题}
{id:1,huifuzhuangtai:1,title:标题}
{id:1,huifuzhuangtai:1,title:标题}
{id:1,huifuzhuangtai:1,title:标题}
{id:1,huifuzhuangtai:1,title:标题}
{id:1,huifuzhuangtai:1,title:标题}

2、我用管道筛选的数据,

def zong2():
    pipeline=[
        {'$group':{'_id':'$huifuzhuangtai','counts':{'$sum':1}}}
    ]
    for i in item_info.aggregate(pipeline):
        data={
                'name':i['_id'],
                'counts':i['counts']
        }
        print(data)
zong2()

得到的结果是

{'name': 1, 'counts': 5}
{'name': 0, 'counts': 1}

我想得到的结果是
{'已回复': 1, 'counts': 5}
{'未回复': 0, 'counts': 1}
这个怎么弄,请求大侠帮忙看看,小白太笨了

慕容3067478
浏览 501回答 1
1回答

翻过高山走不出你

def zong2(): pipeline=[ {'$group':{'_id':'$huifuzhuangtai','counts':{'$sum':1}}} ] for i in item_info.aggregate(pipeline): data={('未回复','已回复')[i['_id']]:i['counts']} print(data) zong2() 或者 data={ ('未回复','已回复')[i['_id']]:i['_id'], 'counts':i['counts'] }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python