python - 获取遗传算法中使用的对象的值

标签 python python-3.x bioinformatics genetic-algorithm objectinstantiation

 @staticmethod
def _select_tournament_population(pop):
    tournament_pop = Population(0)
    i = 0
    while i < TOURNAMENT_SELECTION_SIZE:
        tournament_pop.get_chromosomes().append(pop.get_chromosomes()[random.randrange(0, POPULATION_SIZE)])
        i += 1
    tournament_pop.get_chromosomes().sort(key=lambda x: x.get_fitness(), reverse=True)
    return tournament_pop

def _print_population(self, pop, gen_number):
    print("\n------------------------------------------------")
    print("Generation #", gen_number, "| Fittest chromosome fitness:", pop.get_chromosomes()[0].get_fitness())
    print("Target Chromosome:", TARGET_CHROMOSOME)
    print("------------------------------------------------")
    i = 0
    for x in pop.get_chromosomes():
        print("Chromosome #", i, " :", x, "| Fitness: ", x.get_fitness())
        i += 1

chromosome fitness terminal output

我试图获取对象的值,但我收到了对象在内存中的位置:

('Chromosome #', 0, ' :', <__main__.Chromosome instance at 0x7f7c81a41050>, '| Fitness: ', 10)

最佳答案

<__main__.Chromosome instance at 0x7f7c81a41050>是类 x 的实例。我假设你想要类的变量。通过输入 print(vars(x)) 获取类 x 的变量的字典在 for 循环内查看可用变量,然后访问所需的变量。

关于python - 获取遗传算法中使用的对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48321543/

相关文章:

python,打开的文件会被关闭吗?

python - cx_freeze 在 Windows 下看不到 sqlalchemy?

perl - 仅从 fasta 文件中提取第一个序列

bioinformatics - 使用bed文件提取fasta序列时如何获取strand

具有指定开始和结束的序列的 Ruby 正则表达式

python - 禁用某些消息的格式

python - 了解 Python 类实例

python - 使用逻辑与运算符在 Pandas 中创建 DataFrame

python - inspect.currentframe() 在某些实现下可能不起作用?

python - PyOpenSSL 与 ffi.from_buffer(b"") 错误