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

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

如果第二個(gè)鍵存在,則首先匹配兩個(gè)字典并更改鍵

如果第二個(gè)鍵存在,則首先匹配兩個(gè)字典并更改鍵

回首憶惘然 2022-12-06 16:37:38
我有兩個(gè)dict:a={'a':'A','b':'B'} b={'a':123,'b':123}我需要檢查 b 中的鍵 'a' 和 'b'(例如兩個(gè)元素,在實(shí)際代碼中,它會(huì)更多)是否dict存在于dicta 中。如果是這樣,我應(yīng)該dict使用 a 中的值更改 b 中的鍵dict:預(yù)期結(jié)果:b={'A':123, 'B': 123}我該怎么做?
查看完整描述

3 回答

?
人到中年有點(diǎn)甜

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

{a[k] if k in a else k: v for k, v in b.items()}



查看完整回答
反對(duì) 回復(fù) 2022-12-06
?
慕后森

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

由于configParser在大多數(shù)情況下都像 dict 一樣,我們?cè)谶@里可以做的是使用key不同的代碼塊來確保有唯一的塊。


這是一個(gè)簡(jiǎn)單的測(cè)試來展示它的實(shí)際效果:


import configparser

class Container:

    configs=[] # keeps a list of all initialized objects.

    def __init__(self, **kwargs):

        for k,v in kwargs.items():

            self.__setattr__(k,v) # Sets each named attribute to their given value.

        Container.configs.append(self)


# Initializing some objects.

Container(

    Name="Test-Object1",

    Property1="Something",

    Property2="Something2",

    Property3="Something3",

 )

Container(

    Name="Test-Object2",

    Property1="Something",

    Property2="Something2",

    Property3="Something3",

 )

Container(

    Name="Test-Object2",

    Property1="Something Completely different",

    Property2="Something Completely different2",

    Property3="Something Completely different3",

 )

config = configparser.ConfigParser()

for item in Container.configs: # Loops through all the created objects.

    config[item.Name] = item.__dict__ # Adds all variables set on the object, using "Name" as the key.


with open("example.ini", "w") as ConfigFile:

    config.write(ConfigFile)

在上面的示例中,我創(chuàng)建了三個(gè)包含要由 configparser 設(shè)置的變量的對(duì)象。但是,第三個(gè)對(duì)象Name與第二個(gè)對(duì)象共享變量。這意味著第三個(gè)將在寫入 .ini 文件時(shí)“覆蓋”第二個(gè)。


例子.ini :


[Test-Object1]

name = Test-Object1

property1 = Something

property2 = Something2

property3 = Something3


[Test-Object2]

name = Test-Object2

property1 = Something Completely different

property2 = Something Completely different2

property3 = Something Completely different3


查看完整回答
反對(duì) 回復(fù) 2022-12-06
?
楊魅力

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

到目前為止,其他答案忽略了希望代碼執(zhí)行以下操作的問題:


為字典 a 中的值更改 b 中字典中的鍵


我推斷 中b沒有替換鍵的任何數(shù)據(jù)a都應(yīng)該單獨(dú)保留。因此,遍歷a創(chuàng)建新字典的鍵是c行不通的。我們需要b直接修改。一個(gè)有趣的方法是通過pop()我們通常與列表關(guān)聯(lián)但也適用于字典的方法:


a = {'a': 'A', 'b': 'B'}

b = {'a': 123, 'b': 124, 'C': 125}


for key in list(b):  # need a *copy* of old keys in b

    if key in a:

        b[a[key]] = b.pop(key)  # copy data to new key, remove old key


print(b)

輸出


> python3 test.py

{'C': 125, 'A': 123, 'B': 124}

>


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

添加回答

舉報(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)