Python2.7:确保3D对象列表是唯一的

时间:2016-03-24 00:45:35

标签: python list object memory

我在Python中使用3D对象列表来组织我拥有的数据集。我注意到我的对象元素不是很独特,即数组的一个对象内的变量正在其他对象中更新。说明我的问题的最小工作示例如下所示。

class myClass:
        def __init__(self,ListA=[],ListB=[],ListCount=0):
                self.ListA = ListA
                self.ListB = ListB
                self.ListCount = ListCount

        def add_data(self,ElementA,ElementB):
                self.ListA.append(ElementA)
                self.ListB.append(ElementB)
                self.ListCount = self.ListCount + 1

        def check_data(self):
                if(len(self.ListA) != self.ListCount):
                        print("Error: Unaccounted for Data in Object!")
                        print("Object has %d elements. Thinks it has %d"%(len(self.ListA),self.ListCount))

使用此类定义的python代码就是这样的。

import random

ObjectArray = [[[myClass() for k in range(4)] for j in range(3)] for i in range(2)]
#Print addresses of object array
print("Addresses of the individual object elements in the 3D list ")
for i in range(0,2):
        for j in range(0,3):
                for k in range(0,4):
                        print(ObjectArray[i][j][k])

#Loop over object array and fill each object with some data
for i in range(0,2):
        for j in range(0,3):
                for k in range(0,4):

                        NumData = int(10*random.random())

                        for m in range(0,NumData):

                                #Generate some junk data to insert into the list of this object
                                ListAData = int(10*random.random())
                                ListBData = int(10*random.random())

                                ObjectArray[i][j][k].check_data()
                                ObjectArray[i][j][k].add_data(ListAData,ListBData)
                                ObjectArray[i][j][k].check_data()

我错过了什么?是否可以在Python 2.7中使用3D对象列表?当我打印出来时,列表中的每个对象看起来都有一个唯一的内存地址。任何见解将不胜感激。

此代码的输出示例为:

Addresses of the individual object elements in the 3D list 
<__main__.myClass instance at 0x7fc59ad4ef80> <__main__.myClass
 instance at 0x7fc59ad635a8> <__main__.myClass instance at
 0x7fc59ad63248> <__main__.myClass instance at 0x7fc59ad63440>
 <__main__.myClass instance at 0x7fc59ad6d320> <__main__.myClass
 instance at 0x7fc59ad6d368> <__main__.myClass instance at
 0x7fc59ad6d3b0> <__main__.myClass instance at 0x7fc59ad6d3f8>
 <__main__.myClass instance at 0x7fc59ad6d488> <__main__.myClass
 instance at 0x7fc59ad6d4d0> <__main__.myClass instance at
 0x7fc59ad6d518> <__main__.myClass instance at 0x7fc59ad6d560>
 <__main__.myClass instance at 0x7fc59ad6d638> <__main__.myClass
 instance at 0x7fc59ad6d680> <__main__.myClass instance at
 0x7fc59ad6d6c8> <__main__.myClass instance at 0x7fc59ad6d710>
 <__main__.myClass instance at 0x7fc59ad6d7a0> <__main__.myClass
 instance at 0x7fc59ad6d7e8> <__main__.myClass instance at
 0x7fc59ad6d830> <__main__.myClass instance at 0x7fc59ad6d878>
 <__main__.myClass instance at 0x7fc59ad6d908> <__main__.myClass
 instance at 0x7fc59ad6d950> <__main__.myClass instance at
 0x7fc59ad6d998> <__main__.myClass instance at 0x7fc59ad6d9e0> 
Error: Unaccounted for Data in Object! Object has 4 elements. Thinks it has 0
Error: Unaccounted for Data in Object! Object has 5 elements. Thinks it has 1 
Error: Unaccounted for Data in Object! Object has 5 elements. Thinks it has 1 
Error: Unaccounted for Data in Object! Object has 6 elements. Thinks it has 2 
Error: Unaccounted for Data in Object! Object has 6 elements. Thinks it has 2 
Error: Unaccounted for Data in Object! Object has 7 elements. Thinks it has 3 
Error: Unaccounted for Data in Object! Object has 7 elements. Thinks it has 3 

1 个答案:

答案 0 :(得分:3)

将其更改为

class myClass:
    def __init__(self,ListA=None,ListB=None,ListCount=0):
            self.ListA = [] if ListA is None else ListA
            self.ListB = [] if ListB is None else ListB
            self.ListCount = ListCount

这将避免可变的默认参数

考虑以下

class MyClass:
    def __init__(self,a=[],b=None):
        self.a = a
        print "ID of a:",id(a)
        if b is not None:
           self.a.append(b)

print MyClass(b=5).a
print MyClass(b=6).a
print MyClass(b=7).a

请注意,每个MyClass都有一个唯一的内存位置,但是所有3个指向同一个数组(a的默认参数)