Python - 为对象提供列表

Python - feed an object with a list

所以我有以下代码,它将两个文本文件(字符串)合并到一个列表中。然后这个列表应该提供一个对象。

cat file1.txt

['2.02', '-3.88', '15.25']
['4.40', '-2.36', '14.97']
['5.44', '0.34', '7.34']
['5.76', '0.41', '7.60']
['5.35', '0.19', '13.95']

cat file2.txt

['P1']
['P2']
['P3']
['P4']
['P5']

Code

from ast import literal_eval

#combine both files and return a list
def combiner(infile1, infile2):
    with open(infile1) as f1, open(infile2) as f2:
        f1_lists = (literal_eval(line) for line in f1)
        f2_lists = (literal_eval(line) for line in f2)
        for l1, l2 in zip(f1_lists, f2_lists):
            l3 = l1 + l2
            return l3


class Compass:

def __init__(self, coordx, coordy, coordz, positivity):
        self.coordX = coordx
        self.coordY = coordy
        self.coordZ = coordz
        self.posit = posit

def main():
    file1 = "file1.txt"
    file2 = "file2.txt"
    args = combiner(file1, file2)
    c = Compass(*args)
    print c.coordX + ' ' + c.coordY + ' ' + c.coordZ + ' ' + c.posit

if __name__ == '__main__':
    main()

output

2.02 - 3.88 15.25 P1

它当然只输出第一个列表。在列表结束之前,你会怎么做才能继续喂食? 也许使用 file1.txt?

中的行
from ast import literal_eval

with open(filename) as infile_1, open(filename2) as infile_2:  #Open Both files
    for a, b in zip(infile_1, infile_2):                       #Read each line in parallel
        val =  literal_eval(a) + literal_eval(b)
        print(", ".join(val))

输出:

2.02, -3.88, 15.25, P1
4.40, -2.36, 14.97, P2
5.44, 0.34, 7.34, P3
5.76, 0.41, 7.60, P4
5.35, 0.19, 13.95, P5

您在循环的第一次迭代中返回了 l3,这就是为什么您在输出中只得到一行的原因。

from ast import literal_eval

#combine both files and return a list
def combiner(infile1, infile2):
    with open(infile1) as f1, open(infile2) as f2:
        f1_lists = (literal_eval(line) for line in f1)
        f2_lists = (literal_eval(line) for line in f2)

        result = [] # Store the results in a list
        for l1, l2 in zip(f1_lists, f2_lists):
            l3 = l1 + l2
            result.append(l3)
        return result # Return the list


class Compass:

    def __init__(self, coordx, coordy, coordz, positivity):
            self.coordX = coordx
            self.coordY = coordy
            self.coordZ = coordz
            self.posit = positivity

def main():
    file1 = "file1.txt"
    file2 = "file2.txt"
    all_args = combiner(file1, file2)
    for args in all_args: 
        c = Compass(*args)
        print(c.coordX + ' ' + c.coordY + ' ' + c.coordZ + ' ' + c.posit)

if __name__ == '__main__':
    main()

第一个问题是您 return 只是 combiner() 列表中的第一行。解决方案是制作一个列表 l3,然后在循环耗尽后继续向其追加新行和 return 它。

第二个问题是你有5个坐标和positivities,因此你还需要5个Compass实例。解决方案是创建一个实例列表。

解决上述问题,您的代码将如下所示:

from ast import literal_eval

#combine both files and return a list
def combiner(infile1, infile2):
    with open(infile1) as f1, open(infile2) as f2:
        l3 = [] # this is a list for storing all the lines from both files
        f1_lists = (literal_eval(line) for line in f1)
        f2_lists = (literal_eval(line) for line in f2)
        for l1, l2 in zip(f1_lists, f2_lists):
            l3.append(l1 + l2)
        return l3 # Return all the lines from both text files


class Compass:

    def __init__(self, coordx, coordy, coordz, positivity):
            self.coordX = coordx
            self.coordY = coordy
            self.coordZ = coordz
            self.posit = positivity

def main():
    file1 = "file1.txt"
    file2 = "file2.txt"
    args = combiner(file1, file2)
    # You have 5 different Compass coordinates, therefore you need 5 different Compass instances
    # Therefore make a list of Compass instances
    compasslist = [ Compass(args[i][0], args[i][1], args[i][2], args[i][3]) for i in range(len(args))]

    # args[i][0] is coordx of i-th line (file1.txt), args[i][1] is coordy of i-th line (file1.txt),
    # args[i][2] is coordz of i-th line (file1.txt), and args[i][3] is positivity of i-th line (file2.txt)

    # Lets print all the Compass instances
    for i in range(len(args)):
        print compasslist[i].coordX + ' ' + compasslist[i].coordY + ' ' + compasslist[i].coordZ + ' ' + compasslist[i].posit

if __name__ == '__main__':
    main()