为了账号安全,请及时绑定邮箱和手机立即绑定

如何从json中提取值并递增

如何从json中提取值并递增

冉冉说 2023-03-16 17:13:46
示例 json 如下。我想将id已完成的(假和真)保存到单独的字典中todos = [{'userId': 1, 'id': 1, 'title': 'delectus aut autem', 'completed': False}, {'userId': 1, 'id': 2, 'title': 'quis ut nam facil ', 'completed': False}, {'userId': 1, 'id': 1, 'title': 'fugiat veniam minus', 'completed': False}, {'userId': 1, 'id': 2, 'title': 'et porro tempora', 'completed': True}, {'userId': 1, 'id': 1,'title': 'laprovident illum', 'completed': False}]预期结果如下todos_by_user_true = {1:0,2:1}todos_by_user_false = {1:3,2:1}代码在下面?为什么我的代码不起作用。我得到空白词典todos_by_user_true = {}todos_by_user_false = {}# Increment complete TODOs count for each user.for todo in todos:    if todo["completed"]==True:        try:            # Increment the existing user's count.            todos_by_user_true[todo["id"]] += 1        except KeyError:            # This user has not been seen. Set their count to 1.            todos_by_user_true[todo["id"]] = 0    elif todo["completed"]==False:        try:            # Increment the existing user's count.            todos_by_user_false[todo["id"]] += 1        except KeyError:            # This user has not been seen. Set their count to 1.            todos_by_user_false[todo["id"]] = 0我的字典不对我的输出如下all_by_user_false{1: 2, 2: 0}all_by_user_true{2: 0}免责声明:我还需要处理异常
查看完整描述

1 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

查看您的输入数据,它是这样的: userId 1, id 1 has 0 true, and 3 false userId 1, id 2 has 1 true, and 1 false


给定所需的输出,看起来你真的想使用id而不是userId在你的查找中。id除此之外,第一次在生成的字典中插入时,会出现会计问题。我会这样修复它:


todos_by_user_true = {}

todos_by_user_false = {}

# Increment complete TODOs count for each user.

for todo in todos:

    if todo["completed"]==True:

        try:

            # Increment the existing user's count.

            todos_by_user_true[todo["id"]] += 1

        except KeyError:

            # This user has not been seen. Set their count to 1.

            todos_by_user_true[todo["id"]] = 1

    elif todo["completed"]==False:

        try:

            # Increment the existing user's count.

            todos_by_user_false[todo["id"]] += 1

        except KeyError:

            # This user has not been seen. Set their count to 1.

            todos_by_user_false[todo["id"]] = 1

其中(顺便说一句)已经是您的评论中的内容。


就个人而言,我会在插入之前检查字典中的键,而不是使用try..except,如下所示:


todos_by_user_true = {}

todos_by_user_false = {}

# Increment complete TODOs count for each user.

for todo in todos:

    key = todo["id"]


    if todo["completed"]:  # true case

            # If `id` not there yet, insert it to 0

            if key not in todos_by_user_true:

               todos_by_user_true[key] = 0


            # increment

            todos_by_user_true[key] += 1


    else:  # false case


            # If `id` not there yet, insert it to 0

            if key not in todos_by_user_false:

               todos_by_user_false[key] = 0


            # increment

            todos_by_user_false[key] += 1

这给出了:


todos_by_user_true = {2:1}

todos_by_user_false = {1:3,2:1}

逻辑是这样的,你不能有: todos_by_user_true = {1:0}


当你找到它时,你就计算它的价值;而不是id从单独的列表中迭代。


查看完整回答
反对 回复 2023-03-16
  • 1 回答
  • 0 关注
  • 184 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号