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

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

從字典列表中過濾和提取元素

從字典列表中過濾和提取元素

梵蒂岡之花 2023-03-16 15:48:17
我有一個命名元組列表如下record = [({   "first_name":"nadbor",   "last_name":"drozd",   "occupation":"data scientist",   "markingAgentUsed":[      {         "consumableLabelCode":"L",          "amountUsed":{            "amount" : 100,            "unit": "litre"          }      },     {         "consumableLabelCode":"Y",          "amountUsed":{            "amount" : 300,            "unit": "mililitre"          }      },     {         "consumableLabelCode":"0",          "amountUsed":{            "amount" : 999,            "unit": "gallon"          }      }   ]})][{'first_name': 'nadbor', 'last_name': 'drozd', 'occupation': 'data scientist', 'markingAgentUsed': [{'consumableLabelCode': 'L', 'amountUsed': {'amount': 100, 'unit': 'litre'}}, {'consumableLabelCode': 'Y', 'amountUsed': {'amount': 300, 'unit': 'mililitre'}}, {'consumableLabelCode': '0', 'amountUsed': {'amount': 999, 'unit': 'gallon'}}]}]我想根據(jù) LabelCode 的輸入?yún)?shù)提取值,我想提取兩個值 amount 和 unit。例如,如果我通過 O,我應該得到 999 和加侖作為輸出。
查看完整描述

2 回答

?
青春有我

TA貢獻1784條經(jīng)驗 獲得超8個贊

record = [({

   "first_name":"nadbor",

   "last_name":"drozd",

   "occupation":"data scientist",

   "markingAgentUsed":[

      {

         "consumableLabelCode":"L",

          "amountUsed":{

            "amount" : 100,

            "unit": "litre"

          }

      },

     {

         "consumableLabelCode":"Y",

          "amountUsed":{

            "amount" : 300,

            "unit": "mililitre"

          }

      },

     {

         "consumableLabelCode":"0",

          "amountUsed":{

            "amount" : 999,

            "unit": "gallon"

          }

      }

   ]

})]


userKey = '0'

for entry in record[0]['markingAgentUsed']:

    if entry['consumableLabelCode'] == userKey:

        print("Amount - {}".format(entry['amountUsed']['amount']) )

        print("Units - {}".format(entry['amountUsed']['unit']) )


查看完整回答
反對 回復 2023-03-16
?
四季花海

TA貢獻1811條經(jīng)驗 獲得超5個贊

這一切都是關于理解數(shù)據(jù)結構的嵌套(這不是一個命名的元組,而是一個字典元組列表和一個字典列表,其中一些鍵本身就是字典:-/很少!這段代碼提取了你的內(nèi)容要求:


inp = '0'


for ma in record[0]['markingAgentUsed']:

    if ma['consumableLabelCode'] == inp:

        print(ma['amountUsed']['amount'], ma['amountUsed']['unit'])


查看完整回答
反對 回復 2023-03-16
  • 2 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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