简体   繁体   English

如何在2D数组中正确存储类对象

[英]How to store class objects properly in 2D array

I try simulating cell divisions, and I want to store cells which are part of cell lineages in a 2D array. 我尝试模拟细胞分裂,并且希望将属于细胞谱系一部分的细胞存储在2D阵列中。 Thus the 2D arrays row elements are cells which are part of the lineages which have properties described in "Events" class. 因此,二维数组行元素是单元,它们是沿袭的一部分,具有“事件”类中描述的属性。

Everything works fine, but at the end of my code, where I cannot store properly the class elements. 一切正常,但是在代码末尾,我无法正确存储类元素。

Please help, how can I store these values in Python3 efficiently. 请帮助,如何将这些值有效地存储在Python3中。

Here is the class: 这是课程:


class Events:

    # Initializer / Instance Attributes
    def __init__(self, level, cellNum, Type, time, timeStep, ID, gen, mutNum, childID):
        self.level = level
        self.cellNum = cellNum
        self.Type = Type
        self.time = time
        self.timeStep = timeStep
        self.ID = ID
        self.gen = gen
        self.mutNum = mutNum
        self.childID = childID

Here I store the leafs of the binary tree (cell division tree): 在这里,我存储二叉树(单元划分树)的叶子:


Counter = 0
for i in range(N):
    for k in range(Counter,len(Data)):

        if int(Data[k][0]) == n:
            LeafArray.append(Events(Data[k][0],Data[k][1],Data[k][2],Data[k][3],Data[k][4],Data[k][5],k,0,0))   
            Counter = k+1
            break   

Here I define the LinArray where I want to store the lineage data, and I sort the lineages as the childID of a cell is the same as its child ID starting from a leaf node: 在这里,我定义了要在其中存储谱系数据的LinArray,并对谱系进行排序,因为单元的childID与其从叶子节点开始的子ID相同:


LinArray = []

for i in range(len(LeafArray)):
    lincounter = 0
    LinArrayin = []
    LinArrayin.append(LeafArray[i])

    for j in reversed(range(len(EventVertices))):
        if EventVertices[j].childID==LinArrayin[lincounter].gen:
            lincounter+=1
            LinArrayin.append(EventVertices[j])

        if LinArrayin[lincounter].gen == 0:
            #print(lincounter)
            LinArray.append(LinArrayin)
            print (LinArray[0][1].gen) #error is here
            LinArrayin.clear()
            break

The strangest thing is when I get the error at highlighted line above, which is an index error: 最奇怪的是,当我在上面突出显示的行出现错误时,这是​​一个索引错误:

Traceback (most recent call last): File "HOT_LinTrace.py", line 131, in print (LinArray[0][4].gen) IndexError: list index out of range 追溯(最近一次通话):文件“ HOT_LinTrace.py”,行131,正在打印中(LinArray [0] [4] .gen)IndexError:列表索引超出范围

It also return the proper value of that index. 它还返回该索引的正确值。

Also it is strange that when I want to print out the stored elements of LinArray at the end of code, it does not return anything but the index error message. 同样奇怪的是,当我想在代码末尾打印出LinArray的存储元素时,除了索引错误消息外,它不返回任何内容。

Please help:) 请帮忙:)

I finally could figure it out! 我终于可以弄清楚了!

Python copied just the reference, and as I made LinArrayin = [], LinArray also got erased. Python仅复制了引用,并且当我使LinArrayin = []时,LinArray也被删除了。

So if I change the line: LinArray.append(LinArrayin) 因此,如果我更改行: LinArray.append(LinArrayin)

to: LinArray.append(LinArrayin.copy()) 到: LinArray.append(LinArrayin.copy())

it works:) 有用:)

Hope it helps others as well... 希望它也能帮助其他人...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM