【发布时间】:2018-03-09 11:16:09
【问题描述】:
我希望将 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
【问题讨论】:
-
你之前已经问过这个问题了。
-
不,我没有这略有不同,我第一次得到的答案从未奏效
-
首先你需要决定你希望你的输出是什么。一个列表?一个号码?字典?