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

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

解耦具有相同內(nèi)存地址的字典

解耦具有相同內(nèi)存地址的字典

慕斯王 2021-06-11 22:45:14
我有以下代碼:results = {'location': [], 'analysis_Elem 1': [], 'analysis_Elem 2': [], 'analysis_Elem 3': []}def collectResults(step):    new_results = results    for key in d: #here, d is another dictionary with a lot of results read from .csv file with the same keys as in the results dictionary        for line in d[key]:            if step in line[0]:                new_results[key].append(float(line[-1].strip())/1000)    for line in d[key]:        if step in line[0]:            new_results['location'].append(float(line[-4].strip()))    return new_resultsres1 = collectResults('Time-step 7')res2 = collectResults('Time -step 2')在這個(gè)函數(shù)中,我試圖在一個(gè)字典中滿足 if 語句時(shí)收集結(jié)果,該字典由帶有相應(yīng)空列表的鍵組成。這個(gè)想法是每次調(diào)用函數(shù) collectResults() 時(shí),我都想獲得分配給變量的結(jié)果;比如上面的res1,res2。我遇到的問題是 new_results = results 行導(dǎo)致在第二次調(diào)用該函數(shù)后,字典 new_results(因此也是 res2)包括第二次調(diào)用擴(kuò)展的第一次調(diào)用的結(jié)果。我知道它們具有相同的內(nèi)存地址,這就是覆蓋的原因。對(duì)于列表,可以使用例如 list() 輕松解決。我無法在字典的情況下找到解決方案。需要怎么做才能獲得每次調(diào)用的解耦結(jié)果?
查看完整描述

2 回答

?
紅顏莎娜

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

為什么不在字典上使用 copy() 方法而不是賦值(它只是將新名稱綁定到當(dāng)前字典)?


def collectResults(step):

    new_results = results.copy()


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

添加回答

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