簡體   English   中英

如何更新字典列表中的字典項目

[英]How to updating dict items inside a list of dicts

我有這個我作為主列表維護的字典列表:

orig_list = [
   { 'cpu': '4', 'mem': '4', 'name': 'server1', 'drives': '4', 'nics': '1' }
   { 'cpu': '1', 'mem': '2', 'name': 'server2', 'drives': '2', 'nics': '2' }
   { 'cpu': '2', 'mem': '8', 'name': 'server3', 'drives': '1', 'nics': '1' }
   ]

但是,我需要對這個 dicts 列表中的內容執行操作,例如:

def modifyVM(local_list)
    local_temp_list = []
    for item in local_list :
        '''
        Tons of VM processy things happen here.
        '''
        item['cpu'] = 4
        item['notes'] = 'updated cpu'
    local_temp_list.append(item)
    return local_temp_list

temp_list []
for item in orig_list :
    if item['cpu'] < 4
        temp_list.append(item)

result_list = modifyVM(temp_list)

此時,result_list 包含:

result_list = [
    { 'cpu': '4', 'mem': '2', 'name': 'server2', 'drives': '2', 'nics': '2' }
    { 'cpu': '4', 'mem': '8', 'name': 'server3', 'drives': '1', 'nics': '1' }
    ]

所以我的問題是:

1)什么是更新最有效的方式orig_list與結果result_list 我希望最終得到:

orig_list = [
    { 'cpu': '4', 'mem': '4', 'name': 'server1', 'drives': '4', 'nics': '1' }
    { 'cpu': '4', 'mem': '2', 'name': 'server2', 'drives': '2', 'nics': '2' 'notes': 'updated cpu' }
    { 'cpu': '4', 'mem': '8', 'name': 'server3', 'drives': '1', 'nics': '1' 'notes': 'updated cpu' }
    ]

2) 有沒有辦法在不創建二級列表的情況下更新orig_list

先感謝您。

集合存儲對對象的引用。

因此,您發布的代碼也已經在修改“orig_list”中的項目,因為所有列表都存儲對相同原始詞典的引用。

至於問題的第二部分,您不需要創建新列表。 您可以直接修改對象,下次迭代列表時,您將看到更新后的值。

例如:

orig_list = [
   { 'cpu': 4, 'mem': '4', 'name': 'server1', 'drives': '4', 'nics': '1' },
   { 'cpu': 1, 'mem': '2', 'name': 'server2', 'drives': '2', 'nics': '2' },
   { 'cpu': 2, 'mem': '8', 'name': 'server3', 'drives': '1', 'nics': '1' }
   ]

print orig_list

for item in orig_list :
    if item['cpu'] < 4:
        item['cpu'] = 4

print orig_list

第一次打印的輸出:

[{'mem': '4', 'nics': '1', 'drives': '4', 'cpu': 4, 'name': 'server1'},
 {'mem': '2', 'nics': '2', 'drives': '2', 'cpu': 1, 'name': 'server2'},
 {'mem': '8', 'nics': '1', 'drives': '1', 'cpu': 2, 'name': 'server3'}]

第二次打印:

[{'mem': '4', 'nics': '1', 'drives': '4', 'cpu': 4, 'name': 'server1'},
 {'mem': '2', 'nics': '2', 'drives': '2', 'cpu': 4, 'name': 'server2'},
 {'mem': '8', 'nics': '1', 'drives': '1', 'cpu': 4, 'name': 'server3'}]

不,您不需要創建單獨的列表,只需使用列表理解即可。

只需遍歷列表並檢查cpu鍵的值是否小於 4。如果值小於 4,則將cpu鍵的值更新為 4 並添加一個值為'updated_cpu'的額外關鍵notes 迭代完成后orig_list值就是想要的結果。

>>> orig_list = [{'cpu': 4, 'drives': '4', 'mem': '4', 'name': 'server1', 'nics': '1'},
 {'cpu': 1, 'drives': '2', 'mem': '2', 'name': 'server2', 'nics': '2'},
 {'cpu': 2, 'drives': '1', 'mem': '8', 'name': 'server3', 'nics': '1'}]

>>> for item in orig_list:
        if item['cpu']<4:
            item['cpu']=4
            item['notes'] = 'updated cpu'

>>> orig_list
[{'cpu': 4, 'drives': '4', 'mem': '4', 'name': 'server1', 'nics': '1'},
 {'cpu': 4, 'drives': '2', 'mem': '2', 'name': 'server2', 'nics': '2', 'notes': 'updated cpu'},
 {'cpu': 4, 'drives': '1', 'mem': '8', 'name': 'server3', 'nics': '1', 'notes': 'updated cpu'}]

感謝您的所有投入! 我將 eugenioy 的帖子標記為答案,因為他先發帖。 他和 Rahul Gupta 的答案都是更新字典列表的非常有效的方法。

但是,我一直在嘗試其他方法,因為這些答案盡管有效,但仍然做另一件事,我一直被告知是禁忌:修改您正在迭代的列表。

請記住,我仍在學習 Python。 因此,如果我在這里的一些“啟示”是平凡的,那么它們對我來說是新的並且“哇”。 為此,我添加了我實際最終實施的答案。

這是完成的代碼:

def modifyVM(local_list, l_orig_list)
    for item in local_list[:] :
    l_orig_list.remove(item)
        '''
        Tons of VM processy things happen here.
        '''
        item['cpu'] = 4
        item['notes'] = 'updated cpu'
    l_orig_list.append(item)

temp_list []
for item in orig_list[:] :
    if item['cpu'] < 4
        temp_list.append(item)

modifyVM(temp_list, orig_list)

我改變這一行:

def modifyVM(local_list)

到這一行:

def modifyVM(local_list, l_orig_list)

通過這種方式,我既傳入了我想使用的列表,也傳入了我想更新的列表。

接下來我改變了:

for item in local_list :

到這一行:

for item in local_list[:] :

這會導致“item”遍歷包含所有內容的“local_list”的切片(副本)。

我還補充道:

l_orig_list.remove(item)

和:

l_orig_list.append(item)

這為我解決了幾個問題。

1) 這避免了修改任何正在迭代的列表的可能性。

2) 這允許在進程發生時更新“orig_list”,從而減少創建和維護的“二級列表”。

3) 傳遞給函數的“orig_list”和“l_orig_list”是鏈接變量,直到進行硬分配(即l_orig_list = 'anything')。 (再次感謝所有回答的人!這對我來說是一些很棒的“秘訣”學習,你們都指出了這一點。)所以,避免“=”,我能夠更新“l_orig_list”並有“orig_list”也更新了。

4) 如果需要,這也允許將項目從一個列表移動到另一個列表(即,可以從“orig_list”中刪除產生錯誤的列表項目並將其放置在任何其他列表中,例如“bad_list”。

最后,我要感謝 Steven Rumbalski。 當我讀到你的評論時,我想,“當然!!!” 但是,我花了 2 天的時間才意識到無法對字典進行排序。 我不得不縮小我在這里提出問題所面臨的技術問題的范圍。 排序是腳本其他部分的未說明要求。 這么好的建議,我可能會將它用於另一個腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM