用Python Zip()ping两个生成器

时间:2018-11-11 02:22:37

标签: 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()

1 个答案:

答案 0 :(得分:0)

您正在写入文件,但这是在您要再次读取文件时将“光标”放在文件末尾。

您应该在完成所有写入之后但在执行所有读取之前使用inputs.seek(0)outputs.seek(0)

或者,在写入后关闭打开的文件,然后将其打开以进行读取。