gpt4 book ai didi

Python2.7 : Ensure 3D list of objects are unique

转载 作者:行者123 更新时间:2023-11-30 22:58:28 27 4
gpt4 key购买 nike

我正在使用 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

最佳答案

更改为

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 的默认参数)

关于Python2.7 : Ensure 3D list of objects are unique,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36191351/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com