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

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

Python - 如果 new_list 具有不同的值,則更新 old_list

Python - 如果 new_list 具有不同的值,則更新 old_list

慕運(yùn)維8079593 2021-09-11 17:50:25
我正在嘗試制作一個(gè)腳本,在其中檢查 old_list 和 new_list。如果 new_list 具有與 Old_list 不同的值。如果 old_list 具有比 new_list 更多的值/元素,它將使用 if-elif 語句進(jìn)行檢查,如果有它不應(yīng)該做任何事情,反之亦然。old_list = {'name': 'Hello', 'code': ['Medium', 'Easy', 'Hard']}while True:    new_list = {'name': 'Stackoverflow', 'code': ['Hard', 'Easy']}    try:        if any(i not in old_list['code'] for i in new_list['code']):            if old_list['code'] > new_list['code']:                print("Element removed")                old_list['code'] = new_list['code']            elif old_list['code'] < new_list['code']:                print("New elements added")                old_list['code'] = new_list['code']        else:            randomtime = random.randint(1, 2)            time.sleep(randomtime)            continue    except Exception as err:        randomtime = random.randint(1, 2)        time.sleep(randomtime)        continue輸出應(yīng)該是“元素中刪除”和值應(yīng)更新old_list['code']的['Medium', 'Easy', 'Hard']到['Hard', 'Easy']。但是現(xiàn)在它甚至沒有通過,if any(i not in old_list['code'] for i in new_list['code']):因?yàn)閮蓚€(gè)中的值code都在但 new_list 中沒有“中”,但由于某種原因,我不知道它沒有通過,else而是通過了。如果 new_list 的值/元素比 old_list 多/少,我怎么可能使它更新值,然后打印出元素是否被刪除或添加?
查看完整描述

1 回答

?
天涯盡頭無女友

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

if any(i not in old_list['code'] for i in new_list['code']):


僅當(dāng) 的任何元素new_list['code']不在 中時(shí)才滿足此條件old_list['code']。你的new_list是['Hard','Easy']。兩者都存在于 中old_list,因此條件不滿足并且您的代碼轉(zhuǎn)到該else部分。


如果您只想找出刪除的元素或添加的元素,您只需檢查 new_list 和 old_list 的長(zhǎng)度即可。


if len(old_list['code']) > len(new_list['code']):

    print("Elements removed")

    old_list['code'] = new_list['code']

elif len(old_list['code']) < len(new_list['code']):

    print("New elements added")

    old_list['code'] = new_list['code']

else:

    temp = set(old_list['code']).intersection(set(new_list['code']))

    if len(temp) == len(old_list['code']):

        pass # No change

    else

        print "Elements Removed and Added"

        old_list['code'] = new_list['code']


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

添加回答

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