第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從json中提取值并遞增

如何從json中提取值并遞增

冉冉說 2023-03-16 17:13:46
示例 json 如下。我想將id已完成的(假和真)保存到單獨(dú)的字典中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}]預(yù)期結(jié)果如下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}免責(zé)聲明:我還需要處理異常
查看完整描述

1 回答

?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個贊

查看您的輸入數(shù)據(jù),它是這樣的: userId 1, id 1 has 0 true, and 3 false userId 1, id 2 has 1 true, and 1 false


給定所需的輸出,看起來你真的想使用id而不是userId在你的查找中。id除此之外,第一次在生成的字典中插入時,會出現(xiàn)會計(jì)問題。我會這樣修復(fù)它:


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

其中(順便說一句)已經(jīng)是您的評論中的內(nèi)容。


就個人而言,我會在插入之前檢查字典中的鍵,而不是使用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}


當(dāng)你找到它時,你就計(jì)算它的價值;而不是id從單獨(dú)的列表中迭代。


查看完整回答
反對 回復(fù) 2023-03-16
  • 1 回答
  • 0 關(guān)注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號