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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在for循環(huán)中比較python中JSON對(duì)象的值

在for循環(huán)中比較python中JSON對(duì)象的值

Cats萌萌 2023-06-20 10:26:01
我有一個(gè) JSON 格式的對(duì)象,它是我從 API 主體的用戶那里收到的。當(dāng)在 Python 中存儲(chǔ)并檢查其類(lèi)型時(shí),它顯示為 dict。但是字典中的鍵是作為一個(gè)集合存儲(chǔ)的。x = {'test': {'shipmentInfo': {'Ready Date', 'Ready Time', 'Delivery Date', 'Service Level'}}}我將字典的所有鍵存儲(chǔ)在如下列表中check_list = ["test", "shipmentInfo", "Ready Date","Ready Time","Delivery Date","Service Level"]我正在編寫(xiě)一個(gè)簡(jiǎn)單的條件來(lái)檢查字典中給出的每個(gè)鍵是否都出現(xiàn)在我的列表中。如果任何密鑰不存在,它應(yīng)該說(shuō)密鑰丟失missing = [field for field in x if field not in check_list]   if len(missing) == 0:       print("All values are entered")   else:       [print(f"Missing value: {field}") for field in missing]我的情況的問(wèn)題是,它只是檢查字典中是否存在“測(cè)試”。它沒(méi)有檢查我需要的主要密鑰(“就緒日期”、“就緒時(shí)間”、“交貨日期”、“服務(wù)級(jí)別”)。如果我從列表中刪除一個(gè)值,比如交貨日期("Ready Date","Ready Time","Service Level")我使用的邏輯會(huì)給我這個(gè)結(jié)果All values are entered如何獲取(“就緒日期”、“就緒時(shí)間”、“交貨日期”、“服務(wù)水平”)并將其與我的列表進(jìn)行比較?
查看完整描述

1 回答

?
慕容3067478

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊

這些值{'Ready Date', 'Ready Time', 'Delivery Date', 'Service Level'}組成一個(gè)集合,它們不是內(nèi)部字典的鍵,但仍然可以檢查它們是否存在于原始字典中x:


已實(shí)現(xiàn)的dictionary_to_list函數(shù)采用原始字典x并將其展平為一個(gè)列表,該列表包含列表中的所有鍵和值。


x = {'test': {'shipmentInfo': {'Ready Date', 'Ready Time', 'Delivery Date', 'Service Level'}}}

check_list = ["test", "shipmentInfo", "Ready Date","Ready Time","Delivery Date","Service Level"]



def dictionary_to_list_helper(d, l):

    for k, v in d.items():

        l.append(k)

        if isinstance(v, list) or isinstance(v, set):

            for item in v:

                l.append(item)

        elif isinstance(v, dict):

            dictionary_to_list_helper(v, l)


def dictionary_to_list(d):

    l = []

    dictionary_to_list_helper(d, l)

    return l


missing = [field for field in dictionary_to_list(x) if field not in check_list]

if len(missing) == 0:

   print("All values are entered")

else:

   [print(f"Missing value: {field}") for field in missing]


查看完整回答
反對(duì) 回復(fù) 2023-06-20
  • 1 回答
  • 0 關(guān)注
  • 168 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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