danya tekunov
danya tekunov

Reputation: 113

How to create multiple count of classes instances in Python?

I need to create 189 instances of class.Name that contains string. How can I do that?

Following code does not work.

def naming_of_sequence_neurons():
    letters_list = ["a", "b", "c", "d", "e", "f", "g",
                    "h", "i", "k", "l", "m", "n", "p",
                    "q", "r", "s", "t", "v", "w", "y"]

    input_neuron_web = []

    def naming_cycle(number):

        for j in range(0, 21):
            input_neuron_web[j][number] = Neuron.name = (letters_list[j] + str(number))

    for i in range(0, 9):
        naming_cycle(i)

    return input_neuron_web

inputed = naming_of_sequence_neurons()

for a in range(0, 21):
    for b in range(0, 9):
        print(inputed[a][b].name)

Upvotes: 1

Views: 163

Answers (1)

Samuel Henderson
Samuel Henderson

Reputation: 141

The problem seems to be that you're assigning values to indices that don't exist yet. That won't work for a Python list.

Assuming Neuron is a reasonably well-behaved object, this is likely to work:

def naming_of_sequence_neurons():
    letters_list = ["a", "b", "c", "d", "e", "f", "g",
                    "h", "i", "k", "l", "m", "n", "p",
                    "q", "r", "s", "t", "v", "w", "y"]
    input_neuron_web = []
    for number in range(0, 9):
        new_list = []
        for j in range(0,21):
            new_neuron = Neuron()
            new_neuron.name = letters_list[j] + str(number)
            new_list.append(new_neuron)
        input_neuron_web.append(new_list)
    return input_neuron_web

Upvotes: 1

Related Questions