Zip()在Python中同时ping两个生成器

2024-07-01 07:12:13 发布

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

我有两个非常大的文件。如何将a生成的项目交织在一起,生成一个列表或文件,就像我将两者压缩一样?文件中的每个条目都用换行符隔开。我的代码在下面,它只返回一个空列表。你知道吗

import neat, random, os, itertools

#fast solution for iterating over large wordlist
# playing with the training data a bit
def most(str1):
    count = [(i, str1.count(i)) for i in set(str1)]
    b = True
    r = []
    for item in count:
        r.append(item[1])
    for item in count:
        if item[1] == max(r) and b:
            return ord(item[0])/128
            b = False

def convert(word):
    return [str(ord(i)/122) for i in word]

def read_words(inputfile):
    with open(inputfile, 'r') as f:
        while True:
            buf = f.read(10240)
            if not buf:
                break

            # make sure we end on a space (word boundary)
            while not str.isspace(buf[-1]):
                ch = f.read(1)
                if not ch:
                    break
                buf += ch

            words = buf.split()
            for word in words:
                yield word
        yield '' #handle the scene that the file is empty

asciilist = open(r"C:\Users\Duncan\Downloads\asciilist.txt", "w+")
inputs = open(r"C:\Users\Duncan\Downloads\inputs.txt", "w+")
outputs = open(r"C:\Users\Duncan\Downloads\outputs.txt", "w+")

if __name__ == "__main__":
    for word in read_words(r"C:\Users\Duncan\Downloads\Wordlist.txt"):
        asciilist.write(",".join(convert(word))+"\n")
        outputs.write(str([most(word)]) + "\n")

for item in read_words(r"C:\Users\Duncan\Downloads\asciilist.txt"):
    inputs.write(str([len(item.split(","))]+[0.0 for i in range(1, len(item.split(",")))])+"\n" )

#fitness function
def fitness(genomes, config):
    for genome_id, genome in genomes:
        genome.fitness = 12.0
        net = neat.nn.FeedForwardNetwork.create(genome, config)
        for xi,xo in zip(inputs, outputs): 
            output = net.activate(xi)
            genome.fitness -= (output[0] - xo[0]) ** 2
            print(xi,xo)
print(list(zip(inputs, outputs)))



outputs.close()
inputs.close()
asciilist.close()

Tags: intxtforreadgenomedownloadsitemoutputs
1条回答
网友
1楼 · 发布于 2024-07-01 07:12:13

你正在写你的文件,但这是把“光标”在最后的文件时,你想再次阅读它。你知道吗

在执行所有写入操作之后,但在执行所有读取操作之前,应该使用inputs.seek(0)outputs.seek(0)。你知道吗

或者,在写入后关闭打开的文件,然后打开它们进行读取。你知道吗

相关问题 更多 >

    热门问题