Python对象调用使用了错误的lis

2024-05-19 12:51:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,有以下问题:

每个类“Neuron”都有两个列表(map\u树突,map\u轴突),我想在其中存储来自其他神经元的id。你知道吗

我的问题是,如果'map\u synapses'调用'add\u axon',它将给定的ID写入调用神经元对象的'map\u dendrites'列表中,而不是写入被调用神经元的map中。令人惊讶的是,当调用“add\u dendrites”时,尽管代码是相同的(但首先执行),它却可以工作。你知道吗

有人知道怎么修吗? 谢谢你的支持。你知道吗

# coding=utf-8

class Neuron(object):
    "Klasse Neuron"

    c_dendrites = 1  # Inputs
    c_axons = 1  # Outputs

    c_unmap_dendrites = c_dendrites # Unmapped entspricht am Anfang der Gesamtanzahl der vorhandenen Dednrites
    c_unmap_axons = c_axons # same here but axons

    map_dendrites = [0 for i in range(10)] # Verkabelung Dendrites
    map_axons = [0 for i in range(10)] # Verkabelung Axons

    def __init__(self, id, x, y, z): # 3D-Koordinaten
        "Konstruktor Neuron"

        self.id = id # Jedes Neuron besitzt eine eindeutige ID

        print 'Neuron ID %d (%d/%d/%d) erstellt' % (id, x, y, z)


    def add_dendrite(self, id): # Aus Sicht des Neurons (add_dendrite fügt an ein self.axon ein dendrite des anfragenden Neurons)

        success = False

        if(self.c_unmap_axons != 0):

            #print 'range %d - %d' % (self.c_axons, self.c_unmap_axons)
            print 'Übergebene ID: %d' % id
            #print 'Self ID: %d' % self.id
            #print len(self.map_axons)


            self.map_axons[self.c_axons - self.c_unmap_axons] = id  # Auffüllen von 0 bis self.c_axons
            print 'testX: %d' % self.map_axons[0]
            self.c_unmap_axons -= 1
            success = True

        return success


    def add_axon(self, id): # Aus Sicht des Neurons (add_axon fügt an ein self.dendrite ein Axon des anfragenden Neurons)

        success = False

        if (self.c_unmap_dendrites != 0):

            #print 'range %d - %d' % (self.c_axons, self.c_unmap_axons)
            print 'Übergebene ID: %d' % id
            #print 'Self ID: %d' % self.id
            #print len(self.map_axons)
            print 'test5: %d' % self.map_dendrites[0]
            self.map_dendrites[self.c_dendrites - self.c_unmap_dendrites] = id  # Auffüllen von 0 bis self.c_dendrites
            print 'test6: %d' % self.map_dendrites[0]
# Er nimmt hier die falsche Map
            self.c_unmap_dendrites -= 1
            success = True

        return success


    def map_synapses(self, anzahl_neuronen, ar_neurons):

        import Tools

        print 'map_synapses: Mappe Dendrites'

        # 1. Dendrites verkabeln aus self-Perspektive

        while (0 != self.c_unmap_dendrites):

            # print control1.anzahl_neuronen

            ran_neuron = Tools.randomInt(0, anzahl_neuronen - 1)  # Neuron würfeln

            while (self.id == ran_neuron):  # Gewürfeltes Neuron darf nicht das neuron selbst sein

                ran_neuron = Tools.randomInt(0, anzahl_neuronen - 1)  # Neuron würfeln

            if(ar_neurons[ran_neuron].add_dendrite(self.id)):

                #print 'range %d - %d' % (self.c_dendrites, self.c_unmap_dendrites)
                print 'Speichere %d in map_dendrites an Stelle %d' % (ran_neuron, self.c_dendrites - self.c_unmap_dendrites)
                self.map_dendrites[self.c_dendrites - self.c_unmap_dendrites] = ran_neuron
                print 'test1: %d' % self.map_dendrites[0]
                self.c_unmap_dendrites -= 1
                print 'Dendrite mapped'

        print 'map_synapses: Mappe Dendrites abgeschlossen'

        # 2. Axons verkabeln aus self-Perspektive

        print 'test2: %d' % self.map_dendrites[0]
        print 'map_synapses: Mappe Axons'

        while (0 != self.c_unmap_axons):

            ran_neuron = Tools.randomInt(0, anzahl_neuronen - 1)  # Neuron würfeln

            while (self.id == ran_neuron): # Gewürfeltes Neuron darf nicht das neuron selbst sein

                ran_neuron = Tools.randomInt(0, anzahl_neuronen - 1)  # Neuron würfeln


            print 'test3: %d' % self.map_dendrites[0]

            # 1. Dendrites verkabeln aus self-Perspektive

            print 'ran %d' % ran_neuron

            if (ar_neurons[ran_neuron].add_axon(self.id)): ## add_axon fehlerhaft !!!!!!!!!!!!!!!!!
                print 'test4: %d' % self.map_dendrites[0]
                print 'Speichere %d in map_axons an Stelle %d' % (ran_neuron, self.c_axons - self.c_unmap_axons)
                self.map_axons[self.c_axons - self.c_unmap_axons] = ran_neuron
                self.c_unmap_axons -= 1
                print 'Axon mapped'


        print 'map_synapses: Mappe Axons abgeschlossen'
        print 'map_synpases: ID %d abgeschlossen' % self.id

        self.getStatus()

        return ar_neurons

    def getStatus(self):

        print 'getStatus: Neuron ID %d' % self.id
        print 'getStatus: Dendrites_Map:'
        print ''.join(map(str, self.map_dendrites))
        print 'getStatus: Axons_Map:'
        print ''.join(map(str, self.map_axons))

Tags: selfaddidmapprintneuronranaxon
1条回答
网友
1楼 · 发布于 2024-05-19 12:51:19

它认为问题可能在于变量的范围。在课程开始时,你是这样分配他们的

class Neuron(object):
    c_dendrites = 1  # Inputs
    c_axons = 1  # Outputs
    c_unmap_dendrites = c_dendrites # Unmapped entspricht am Anfang der Gesamtanzahl der vorhandenen Dednrites
    c_unmap_axons = c_axons # same here but axons
    map_dendrites = [0 for i in range(10)] # Verkabelung Dendrites
    map_axons = [0 for i in range(10)] # Verkabelung Axons

但这使得它们只针对类本身,而不特定于任何实例。静态的?你知道吗

您应该使用init函数并使用self来定义变量,这意味着它将绑定到实例,然后在需要使用它们时也使用self。你知道吗

def __init__(self, id, x, y, z): # 3D-Koordinaten
    "Konstruktor Neuron"
    self.c_dendrites = 1  # Inputs
    self.c_axons = 1  # Outputs
    self.c_unmap_dendrites = c_dendrites # Unmapped entspricht am Anfang der Gesamtanzahl der vorhandenen Dednrites
    self.c_unmap_axons = c_axons # same here but axons
    self.map_dendrites = [0 for i in range(10)] # Verkabelung Dendrites
    self.map_axons = [0 for i in range(10)] # Verkabelung Axons
    self.id = id # Jedes Neuron besitzt eine eindeutige ID
    print 'Neuron ID %d (%d/%d/%d) erstellt' % (id, x, y, z)

相关问题 更多 >