python创建没有引用的字典列表(python create list of dictionaries without reference)

编程入门 行业动态 更新时间:2024-10-08 18:40:26
python创建没有引用的字典列表(python create list of dictionaries without reference)

我有一个要求,我需要创建字典对象,并将重复键嵌入到列表对象中,如下所示:

[{ "key": "ABC" },{ "key": "EFG" } ]

我决定将一个顶级列表初始化为空,如outer_list=[]和一个占位符字典对象,如dict_obj= {} 。 接下来,我使用以下步骤继续向列表中添加元素:

使用dict_obj["key"]="ABC"将{ "key": "ABC" }分配给dict_obj 使用outer_list.append(dict_obj)将此对象添加到列表 使用dict_obj.clear()刷新/弹出字典对象中的键/项 根据数据中的键/项组合数重复步骤1到3

问题: outer_list对象维护对原始dict_obj的引用,如果刷新了dict_obj或添加了新的键/项,则会相应地更改。 最后,我最终得到了这个[{ "key": "EFG" },{ "key": "EFG" } ]而不是[{ "key": "ABC" },{ "key": "EFG" } ]

如果可能的话,请指导我一些解决方法。

I have a requirement in which I need create dictionary objects with duplicate keys embedded into a list object, something like this:

[{ "key": "ABC" },{ "key": "EFG" } ]

I decided to have a top level list initialized to empty like outer_list=[] and a placeholder dictionary object like dict_obj= {}. Next I keep adding elements to my list using the following steps:

assign { "key": "ABC" } to dict_obj using dict_obj["key"]="ABC" Add this object to the list using outer_list.append(dict_obj) Flush/pop the key/items in dictionary object using dict_obj.clear() Repeat steps 1 to 3 based on the number of key/item combinations in my data

Issue: the outer_list object maintains a reference to the original dict_obj and if the dict_obj is flushed or a new key/item is added it changes accordingly. So finally, I end up with this [{ "key": "EFG" },{ "key": "EFG" } ] instead of [{ "key": "ABC" },{ "key": "EFG" } ]

Please guide me with some workarounds if possible.

最满意答案

我认为有两种方法可以避免重复引用。

第一种是append字典的副本,而不是对它的引用。 dict实例有一个copy方法,所以这很容易。 只需将当前append调用更改为:

outer_list.append(dict_obj.copy())`

另一个选择是不要总是使用相同的dict_obj对象,而是为每个条目创建一个单独的字典对象。 在此版本中,您将使用以下命令替换对dict_obj.clear()调用:

dict_obj = {}

对于第二种方法,您可以选择重新排序,而不是直接替换。 您可以将设置代码移动到循环的开头,并在循环结束时删除重置逻辑。

也就是说,更改代码如下所示:

outer_list = [] dict_obj = {} for foo in whatever: # add stuff to dict_obj outer_list.append(dict_obj) dict_obj.clear()

至:

outer_list = [] for foo in whatever: dict_obj = {} # add stuff to dict_obj outer_list.append(dict_obj)

如果创建内部词典的逻辑足够简单来计算,您甚至可以将整个事物转换为列表理解:

outer_list = [{"key": value, "key2": value2} for value, value2 in some_sequence]

I think there are two ways to avoid the duplicate references.

The first is to append a copy of the dictionary, instead of a reference to it. dict instances have a copy method, so this is easy. Just change your current append call to:

outer_list.append(dict_obj.copy())`

The other option is to not always use the same dict_obj object, but rather create a separate dictionary object for each entry. In this version, you'd replace your call to dict_obj.clear() with:

dict_obj = {}

For the second approach, you might choose to reorder things rather than doing a straight one-line replacement. You could move the setup code to the start of the loop and get rid of the reset logic at the end of the loop.

That is, change code that looks like this:

outer_list = [] dict_obj = {} for foo in whatever: # add stuff to dict_obj outer_list.append(dict_obj) dict_obj.clear()

To:

outer_list = [] for foo in whatever: dict_obj = {} # add stuff to dict_obj outer_list.append(dict_obj)

If the logic for creating the inner dictionaries is simple enough to compute, you might even turn the whole thing into a list comprehension:

outer_list = [{"key": value, "key2": value2} for value, value2 in some_sequence]

更多推荐

本文发布于:2023-08-04 03:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405384.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   列表   python   create   reference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!