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

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

如何在 JSON 字典列表中附加值?

如何在 JSON 字典列表中附加值?

湖上湖 2023-06-20 14:23:26
我在附加現(xiàn)有詞典的值時(shí)遇到問題。目標(biāo)是打開一個(gè) json 文件,分析現(xiàn)有的字典,查看是否存在任何服務(wù),如果該服務(wù)存在,則附加新密碼。#!/usr/bin/env python3import jsonimport random#a set of characters to chose from for the passwordschar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()\{\}[]-_=+/?.>,<|`~'services = []passlength = 0b = 0#this function takes your input on what services you want in your list of dictionariesdef service_add():    serviceques = input('what service is the password for? ')    service = (serviceques)    if service == 'done':        starter()    elif service == '':        print('you must enter a service name')        service_add()    elif service == ' ':        print('you must enter a service name')        service_add()    else:        if service in services:            print('service and key already exists')        else:            services.append(service)            #print(services)        service_add()#function to tell how long you want your password to bedef starter():    lengths = input('How long do you want the password to be? ')    global length    length = int(lengths)    makingPairs()#this function creates a password and puts the password in a dictionary with each #service in your list then appends the set of service and password to a json filedef makingPairs():      global b    global services    global length          a = 0    jsondics= []    for line in services:        a = a + 1    for x in range(a):        password = ''             for c in range(length):            password += random.choice(char)                jsonpairs = {            'Service' : services[b],            'Password' : password        }這是來自 json 文件的原始數(shù)據(jù)[{"Service": "spotify", "Password": "5QF50W,!UG"}, {"Service": "pandora", "Password": "E=b]|6]-HJ"}]當(dāng)我運(yùn)行代碼并嘗試刪除“潘多拉”時(shí),它給了我這個(gè)例子[{'Service': 'spotify', 'Password': 'bMXa2FY%Rh'}, {'Password': '$m--c<CY2x'}]問題是,它沒有刪除整個(gè)字典,而是只刪除了名為“Pandora”的鍵。我試圖更改new_list變量,但它仍然只刪除鍵或值,而不是整個(gè)變量。
查看完整描述

1 回答

?
炎炎設(shè)計(jì)

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

你是在問如何根據(jù)其中一個(gè)鍵的值從字典列表中刪除一個(gè)項(xiàng)目。你有:

new_list = [{k: v for k, v in d.items() if v != 'pandora'} for d in JsonDictList]

當(dāng)你給它這個(gè)輸入時(shí):

[{"Service": "spotify", "Password": "5QF50W,!UG"}, {"Service": "pandora", "Password": "E=b]|6]-HJ"}]

"Service": "pandora"...當(dāng)您真的希望它刪除整個(gè)字典時(shí),它會(huì)刪除鍵/值對(duì){"Service": "pandora", "Password": "E=b]|6]-HJ"}。您的問題還提到了追加(即在集合末尾添加一些內(nèi)容),但我不清楚您遇到了什么麻煩。所以我只是在回答如何從字典列表中刪除一個(gè)元素。

從具有服務(wù)“潘多拉”的列表中刪除字典

所以首先,我們可以這樣做:

new_list = [d for d in JsonDictList if d['Service'] != 'pandora']

當(dāng)它有一個(gè)名為“Service”的鍵與一個(gè)值“pandora”配對(duì)時(shí),它會(huì)從列表中刪除每個(gè)元素。它還假設(shè)每個(gè)元素都有一個(gè)名為“Service”的鍵,如果其中一個(gè)沒有,則會(huì)導(dǎo)致異常。如果其中一些可能沒有“服務(wù)”密鑰,您可以改為執(zhí)行以下操作:

new_list = [d for d in JsonDictList if d.get('Service') != 'pandora']

從列表中刪除在任何領(lǐng)域都有“潘多拉”的字典

您的示例還將刪除等于“pandora”的密碼。我認(rèn)為這不是故意的。但是如果你確實(shí)想刪除任何以 'pandora' 作為其任何值的字典,你可以這樣做:

new_list = [d for d in JsonDictList if not any(v == 'pandora' for v in d.values())]


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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