如何从python中的textfiles中获取2个数据列表

时间:2018-03-09 11:16:10

标签: python string

我想将AAA.txt的每个元素平方并添加到bbb.txt以创建一个大的新数组,字符串/无论什么叫做c

def mi_func(P):
    f=open(P, 'r')
    first = f.readline()
    restlines= f.readlines()
    f.close()
    return first, restlines


afirst,arest = mi_func('aaa.txt')
bfirst,brest = mi_func('bbb.txt')



arest = [x.lstrip('0').split(',') for x in arest if x != '\n']     #strip it split it get rid of ends of lines
brest = [x.lstrip('0').split(',') for x in brest if x != '\n']

for i in range(len(arest)):                                             
    arest[i] = [float(x) for x in arest[i] if x != '\n' and x!= '']
print(arest)

for i in range(len(brest)):
    s= brest[i] = [float(x) for x in brest[i] if x != '\n' and x != '']


c = 0
for i in range(len(arest)):
    for j in range(len(arest[i])):
        c += (arest[i][j]**2)+(brest[i][j]**2)
print(c)

我只想让c成为另一个,列表或数组或w / e

aaa.txt在

之下
test a line 1

3,6,8,99,-4,0.6,8

0,9,7,5,7,9,5

2,2,2,2,2,2,5

7,5,1,2,12,8,0.9

bbb.txt在

之下
test b line 1

1,2,3,4,5,6,7,8

55,0,90,09,1,2,3,

8,9,7,6,8,7,6

3,43,5,8,2,4,1

1 个答案:

答案 0 :(得分:0)

如果要迭代多个列表,可以使用" zip"。例如

for a,b in zip(listA,ListB):
       c = square(a) + b
       listC.append(c);