繁体   English   中英

如何从 json 中提取值并递增

[英]How to extract value from json and increment

样品 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

我的字典不合适

我的 output 在下面

todos_by_user_false {1: 2, 2: 0}

todos_by_user_true {2: 0}

免责声明:我也需要处理异常

查看您的输入数据,它是这样的:userId 1,id 1 有 0 真,3 假 userId 1,id 2 有 1 真,1 假

鉴于所需的 output,看起来您确实想在查找中使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM