python如何将新对象保存到列表中而不重复(python how to save new objects into a list without duplication)

编程入门 行业动态 更新时间:2024-10-25 22:27:41
python如何将新对象保存到列表中而不重复(python how to save new objects into a list without duplication)

我的计划的高级图片

目的 :解析XML文件并将文本保存到类似的python对象中 问题 :每次我创建一个新的python对象并将其附加到列表时,它似乎都会附加对前一个对象的引用,而不是创建一个新对象。

我的预期结构应该是什么

每个包含连接列表的应用程序列表

app1: connection1 connection2 app2: connection3 connection4 connection5

我怎么知道存在内存问题

当我在属于特定应用程序的一个连接列表中更改一件事时,另一个应用程序中的连接也将更改。 我打印出连接的内存位置,发现有重复的引用(参见内存打印)

内存打印输出

当我打印出应用程序位置时,我得到了以下内容( 它不漂亮,但你可以看到至少地址不同 ):

generator_libraries.data_extraction.extraction.Application_XML instance in 0x15a07e8 - memory location = 22677480 generator_libraries.data_extraction.extraction.Application_XML instance in 0x15a0758 - memory location = 22677336 generator_libraries.data_extraction.extraction.Application_XML instance at 0x15a0830 - memory location = 22677552 generator_libraries.data_extraction.extraction .Application_XML实例位于0x15a0878 - 内存位置= 22677624 generator_libraries.data_extraction.extraction.Application_XML实例位于0x15a08c0 - 内存位置= 22677696 generator_libraries.data_extraction.extraction.Application_XML实例位于0x15a0908 - 内存位置= 22677768 generator_libraries.data_extraction.extraction.Application_XML实例位于0x15a0950 - memory location = 22677840 generator_libraries.data_extraction.extraction.Application_XML instance in 0x15a0998 - memory location = 22677912 generator_libraries.data_extraction.extraction.Application_XML instance in 0x15a09e0 - m emory location = 22677984 generator_libraries.data_extraction.extraction.Application_XML instance in 0x15a0a28 - memory location = 22678056

当我打印出3个不同应用程序的连接位置时,我得到了以下内容( 你可以看到地址之间的重复 ):

app1 :: 记忆位置= 22721168 记忆位置= 22721240 记忆位置= 22721024 记忆位置= 22721600

记忆位置= 22721672

APP 2:

记忆位置= 22721240 记忆位置= 22721672 记忆位置= 22721600 记忆位置= 22721168 记忆位置= 22722104 记忆位置= 22722176

来自内存分析的结论似乎每次我创建一个新的连接对象并将其附加到我的“连接”列表,而不是创建一个新对象时,它会从我以前的对象中获取内存引用。

有问题的函数代码的更详细视图

class Application_XML(XML_Element_Class): name = None connections=copy.deepcopy([]) xml_element=None def get_connections(self): xml_connections = self.get_xml_children() for xml_connection in xml_connections: connection = None ## reset the connection variable connection = Connection_XML(xml_connection) connection.unique_ID = connection_count self.connections.append(copy.deepcopy(connection)) del connection ## reset where its pointing to connection_count+=1 self.log_debugging_info_on_connection_memory() ### this is where I look at memory locations

一个做同样事情的课......但是有效

class Root_XML(XML_Element_Class): applications = copy.deepcopy([]) def get_applications(self): xml_applications = self.get_xml_children() for xml_application in xml_applications: self.applications.append(Application_XML(xml_application)) self.log_application_memory_information()

最后的话

我已经尝试了几乎所有关于创建对象的方法的书中的每一个技巧,在我制作它们之后将它们摧毁......但是还没有任何帮助。 我觉得答案背后可能有一个必不可少的蟒蛇记忆概念...但是在我在网上搜索之后,没有任何东西可以解释答案。

拜托,如果你能提供帮助那就太棒了!!! 谢谢 :)

High-level picture of my program

purpose: parse an XML file and save text into similar python objects problem: Every time I create a new python object and append it to a list, instead of creating a new object it seems to append a reference to the previous objects.

Summary of what my intended structure should be:

list of applications that each contains a list of connections

app1: connection1 connection2 app2: connection3 connection4 connection5

How I know there is a memory problem:

When I change one thing in one list of connections that belong to a particular application, the connections in another application will also change. I printed out the memory locations for the connections and found that there are duplicate references (see memory prints)

Memory print outs

when I printed out application locations I got the following (its not pretty, but you can see that at least the addresses are different):

generator_libraries.data_extraction.extraction.Application_XML instance at 0x15a07e8 - memory location = 22677480 generator_libraries.data_extraction.extraction.Application_XML instance at 0x15a0758 - memory location = 22677336 generator_libraries.data_extraction.extraction.Application_XML instance at 0x15a0830 - memory location = 22677552 generator_libraries.data_extraction.extraction.Application_XML instance at 0x15a0878 - memory location = 22677624 generator_libraries.data_extraction.extraction.Application_XML instance at 0x15a08c0 - memory location = 22677696 generator_libraries.data_extraction.extraction.Application_XML instance at 0x15a0908 - memory location = 22677768 generator_libraries.data_extraction.extraction.Application_XML instance at 0x15a0950 - memory location = 22677840 generator_libraries.data_extraction.extraction.Application_XML instance at 0x15a0998 - memory location = 22677912 generator_libraries.data_extraction.extraction.Application_XML instance at 0x15a09e0 - memory location = 22677984 generator_libraries.data_extraction.extraction.Application_XML instance at 0x15a0a28 - memory location = 22678056

when I printed out connection locations for 3 different applications I got the following (you can see the duplication among addresses):

app1:: memory location = 22721168 memory location = 22721240 memory location = 22721024 memory location = 22721600

memory location = 22721672

app2:

memory location = 22721240 memory location = 22721672 memory location = 22721600 memory location = 22721168 memory location = 22722104 memory location = 22722176

conclusions from memory analysis It seems that every time I create a new connection object and append it to my "connections" list, instead of creating a new object, it takes the memory reference from my previous objects.

A more detailed view of the problematic function's code

class Application_XML(XML_Element_Class): name = None connections=copy.deepcopy([]) xml_element=None def get_connections(self): xml_connections = self.get_xml_children() for xml_connection in xml_connections: connection = None ## reset the connection variable connection = Connection_XML(xml_connection) connection.unique_ID = connection_count self.connections.append(copy.deepcopy(connection)) del connection ## reset where its pointing to connection_count+=1 self.log_debugging_info_on_connection_memory() ### this is where I look at memory locations

A class that does the same thing... but works

class Root_XML(XML_Element_Class): applications = copy.deepcopy([]) def get_applications(self): xml_applications = self.get_xml_children() for xml_application in xml_applications: self.applications.append(Application_XML(xml_application)) self.log_application_memory_information()

Final Words

I have tried nearly every trick in the book in terms of alternate ways of creating the objects, destroying them after I make them... but nothing has helped yet. I feel that there may be an essential python memory concept behind the answer... but after all my searching online, nothing has shed any light onto the answer.

Please, if you can help that would be awesome!!! Thanks :)

最满意答案

我不认为问题与您正在查看的部分有关,而是与Connection_XML类有关:

class Connection_XML(XML_Element_Class): ### members name = None type = None ID = None max_size = None queue_size = None direction = None def do_something(self): pass

所有这些成员都是类属性 。 每个Connection_XML实例共享一个name ,一个type等等。因此,即使您的实例都是唯一对象,更改一个也会更改所有实例。

您需要实例属性 - 每个实例的单独name等。 你这样做的方法是通常在__init__方法中动态创建属性:

class Connection_XML(XML_Element_Class): def __init__(self): self.name = None self.type = None self.ID = None self.max_size = None self.queue_size = None self.direction = None def do_something(self): pass

没有真正的SSCCE,很难确定这是你的问题。 在这个玩具示例中,所有属性都具有值None ,这是不可变的,因此它不会真正导致这些类型的问题。 但是,如果其中一个是一个列表,或者一个具有自己属性的对象,它就会。

I don't think the problem has anything to do with the part you're looking at, but rather with the Connection_XML class:

class Connection_XML(XML_Element_Class): ### members name = None type = None ID = None max_size = None queue_size = None direction = None def do_something(self): pass

All of those members are class attributes. There's a single name shared by every Connection_XML instance, a single type, etc. So, even if your instances are all unique objects, changing one changes all of them.

You want instance attributes—a separate name, etc., for each instance. The way you do that is to just create the attributes dynamically, usually in the __init__ method:

class Connection_XML(XML_Element_Class): def __init__(self): self.name = None self.type = None self.ID = None self.max_size = None self.queue_size = None self.direction = None def do_something(self): pass

It's hard to be sure this is your problem without a real SSCCE. In this toy example, all of the attributes have the value None, which is immutable, so it won't really lead to these kinds of problems. But if one of them is, say, a list, or an object that has its own attributes, it will.

更多推荐

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

发布评论

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

>www.elefans.com

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