简体   繁体   中英

Python: TypeError: 'function' object has no attribute '__getitem__'

I'm new to python, and I'm having trouble with some code. I'm getting the error

TypeError: 'function' object has no attribute '__getitem__' at line 32

I can't figure out what's going on. From what I've seen on the internet, it means something about using a function as a list, but I can't figure that out with my code. My code is

#! /usr/local/bin/python


import random


def genFirstGen():
    print "First Generation"
    generation = []
    for chromosomecounter in range(0, 20):
        chromosome = []
        fitness = 0
        for genecounter in range(0, 10):
            chromosome.append(random.randrange(0, 2))
        for genecounter in range(0, 10):
            if (chromosome[genecounter] == 1):
                fitness += 1
        chromosome.append(fitness)
        generation.append(chromosome)
        print gene
    return generation


def FindMostFit(generation):
    failedset = [0,0,0,0,0,0,0,0,0,0,0]
    highestfitness = 0
    mostfit = 0
    fitparents = []
    gene = []
    for fitcounter in range(0, 10):
        for chromosomecounter in range(0, 20):
            chromosome = [generation[chromosomecounter]] #  The error is here.
            fitness  = chromosome[10]
            highestfitness = 0
            if (fitness > highestfitness):
                highestfitness = fitness
                mostfit = chromosomecounter

        fitparents.append(generation[mostfit])
        generation[mostfit] = failedset
    return fitparents


def BreedMostFit(Parents):
    Mother = Parents(random.randrange(0, 10))
    Father = Parents(random.randrange(0, 10))
    child = []
    for genecounter in range(0, 5):
        parentgenetaken = random.randrange(0, 2)
        if (parentgenetaken == 1):
            child.append(Mother[genecounter * 2])
            child.append(Mother[(genecounter * 2) + 1])
        else:
            child.append(Father[genecounter * 2])
            child.append(Father[(genecounter * 2) + 1])
    return child


def Generation2():
    newgeneration = []
    for gencounter in range(0, 20):
        newgeneration.append(BreedMostFit(FindMostFit(genFirstGen)))
    return newgeneration

def GenerationFunction(parentgen):
    generation = []
    for gencounter in range(0, 20):
        generation.append(BreedMostFit(FindMostFit(parentgen)))
    return generation

GenerationFunction(Generation2())

I'm at my wits end. Can anyone help? Thanks!

On line 62 you pass genFirstGen function into FindMostFit

newgeneration.append(BreedMostFit(FindMostFit(genFirstGen)))

Later on line 32 you index it. I guess you wanted to call the function and pass its results instead. So replace line 62 with

newgeneration.append(BreedMostFit(FindMostFit(genFirstGen())))

What's passed to FindMostFit is genFirstGen function itself, not an output from called genFirstGen() .

This:

newgeneration.append(BreedMostFit(FindMostFit(genFirstGen)))

Should look like this:

newgeneration.append(BreedMostFit(FindMostFit(genFirstGen()))) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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