如何更新dict列表中的dict项

2024-04-24 09:36:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个目录列表,作为一个主列表:

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' }
   ]

但是,我需要对dict列表中的内容执行操作,例如:

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 = [
    { 'cpu': '4', 'mem': '2', 'name': 'server2', 'drives': '2', 'nics': '2' }
    { 'cpu': '4', 'mem': '8', 'name': 'server3', 'drives': '1', 'nics': '1' }
    ]

所以我的问题是:

1)用result_list的结果更新orig_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?你知道吗

先谢谢你。你知道吗


Tags: name列表localcpuitemmemtemplist
3条回答

感谢大家的投入!我把尤金尼奥的帖子标为答案,因为他是第一个发帖的。他和拉胡尔古普塔的答案都是更新词典列表的非常有效的方法。你知道吗

然而,我一直在尝试其他方法,因为这些答案虽然有效,但仍然会做另一件我一直被告知是禁忌的事情:修改您正在迭代的列表。你知道吗

记住,我还在学习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\u list”的片段(副本)。你知道吗

我还补充说:

l_orig_list.remove(item)

以及:

l_orig_list.append(item)

这为我解决了几个问题。你知道吗

1)这避免了修改正在迭代的任何列表的可能性。你知道吗

2)这允许“原始列表”随着流程的发生而更新,从而减少了创建和维护的“辅助列表”。你知道吗

3)传递到函数中的“orig\u list”和“l\u orig\u list”是链接变量,直到进行硬赋值(即l\u orig\u list='任何')。(再次感谢所有回答的人!这对我来说是个很好的“秘方”,你们都指出了。)所以,避免使用“=”,我可以更新“原始列表”,也可以更新“原始列表”。你知道吗

4)这还允许在需要时将项目从一个列表移动到另一个列表(即,生成错误的列表项目可以从“原始列表”中删除并放置在任何其他列表中,例如“坏列表”。你知道吗

最后,我要感谢史蒂文·伦巴尔斯基。当我读到你的评论时,我想,“当然!!!”然而,我花了两天的时间才意识到字典是无法分类的。我不得不缩小我在这里提出问题所面临的技术问题的范围。排序对于脚本的其他部分是一个未明确说明的要求。这么好的建议,我可能会用它来写另一个剧本。你知道吗

集合存储对对象的引用。你知道吗

因此,您发布的代码已经在修改“orig\u 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'}]

相关问题 更多 >