Python 如何将两个不同文件中的数字相乘?
我有两个不同的文件A和B:Python 如何将两个不同文件中的数字相乘?,python,Python,我有两个不同的文件A和B: A= 5 2 3 4 6 78 .. B= 3 4 2 1 8 7 .. 我需要计算两个文件中每个文件的乘法,我使用以下代码: A_file=open(r'C:\Users\user\Desktop\New_folder\A.txt', 'r') B_file = open(r'C:\Users\user\Desktop\New_folder\B.txt', 'r') for a in A_f
A= 5
2
3
4
6
78
..
B= 3
4
2
1
8
7
..
我需要计算两个文件中每个文件的乘法,我使用以下代码:
A_file=open(r'C:\Users\user\Desktop\New_folder\A.txt', 'r')
B_file = open(r'C:\Users\user\Desktop\New_folder\B.txt', 'r')
for a in A_file:
for line, b in enumerate(B_file):
#print b
print (a,'+',b)
c= int(a)*int(b)
print (c)
结果是:
('5\n', '+', '3\n')
15
('5\n', '+', '4\n')
20
('5\n', '+', '2\n')
10
('5\n', '+', '1\n')
5
('5\n', '+', '8\n')
40
('5\n', '+', '7')
35
('5\n', '+', '3\n')
15
('2\n', '+', '4\n')
8
('3\n', '+', '2\n')
6
('4\n', '+', '1\n')
4
('6\n', '+', '8\n')
48
('78\n', '+', '7')
546
但预测结果是:
('5\n', '+', '3\n')
15
('5\n', '+', '4\n')
20
('5\n', '+', '2\n')
10
('5\n', '+', '1\n')
5
('5\n', '+', '8\n')
40
('5\n', '+', '7')
35
('5\n', '+', '3\n')
15
('2\n', '+', '4\n')
8
('3\n', '+', '2\n')
6
('4\n', '+', '1\n')
4
('6\n', '+', '8\n')
48
('78\n', '+', '7')
546
请问如何解决这个问题 对zip中的a,b使用
(a_文件,b_文件):
并行浏览文件。考虑到两个文件的长度相等
A_file=open(r'C:\Users\user\Desktop\New_folder\A.txt', 'r')
B_file = open(r'C:\Users\user\Desktop\New_folder\B.txt', 'r')
alist=[]
blist=[]
for each in A_file:
alist+=[int(each)]
for each in B_file:
blist+=[int(each)]
if len(alist)==len(blist):
for every in alist:
print (alist[every],'+',blist[every])
c= alist[every]+blist[every]
print (c)
所谓预测结果,是指你想要的结果吗?如果文件不大,只需将它们加载到内存中,然后执行操作。否则,只需循环一次。现在,对于文件A中的每一行,您正在执行文件B中的所有行,这是基于代码的预测结果;虽然..@Ev.Kounis文件很大,但可能不是我想要的,我在每个文件中有超过200000行,我给他们的只是一个简单的例子,说明我需要做什么。@JunbangHuang是的,预测结果,是我想要的。