Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python txt文件中整数的总和_Python_String_Text_Indexing_Int - Fatal编程技术网

Python txt文件中整数的总和

Python txt文件中整数的总和,python,string,text,indexing,int,Python,String,Text,Indexing,Int,我有两个只包含数字(每行数字)的txt文件,因此我想将第1行+第1行依次添加到每个文件的最后一行。每个文件都有相同的行号 我正在尝试这种方法,但是我只能打印第一行 arq = open ("List1.txt") arq2 = open ("List2.txt") x = [linha.strip() for linha in arq] arq.close() y = [linha.strip() for linha in arq2] arq2.close() for linha in x:

我有两个只包含数字(每行数字)的txt文件,因此我想将第1行+第1行依次添加到每个文件的最后一行。每个文件都有相同的行号

我正在尝试这种方法,但是我只能打印第一行

arq = open ("List1.txt")
arq2 = open ("List2.txt")

x = [linha.strip() for linha in arq]
arq.close()
y = [linha.strip() for linha in arq2]
arq2.close()

for linha in x:
    index = 0
    while index<len(x):
       result = (int(x[index]) + int(y[index]))
       index += 1
    print result
arq=open(“List1.txt”)
arq2=打开(“List2.txt”)
x=[linha.strip()用于arq中的linha]
arq.close()
y=[linha.strip()表示arq2中的linha]
arq2.close()
对于x中的linha:
索引=0

而index则可以使用
zip
轻松实现这一点

将相关代码行更改为:

for x, y in zip(arq, arq2):
    result = int(x) + int(y)
    print(result)

您可以使用
zip
轻松实现这一点

将相关代码行更改为:

for x, y in zip(arq, arq2):
    result = int(x) + int(y)
    print(result)

首先我会得到一个numpy数组并连接起来

import numpy as np
file1 = np.genfromtxt('file1')
file2 = np.genfromtxt('file2')
file3 = np.concatenate((file1, file2), axis=1)

然后按您认为合适的方式保存

首先,我将获得一个numpy数组并连接

import numpy as np
file1 = np.genfromtxt('file1')
file2 = np.genfromtxt('file2')
file3 = np.concatenate((file1, file2), axis=1)

然后按您认为合适的方式保存

,而您可能应该像上面提到的那样使用
zip
,如果您想继续使用您的做事方式,您的问题就在这里

当您在python中执行
for
循环时,它是一个foreach,所以当您这样做时

for linha in x:
    print linha
linha
将是
x[n]
的值,其中n是循环的迭代

无论如何,要修复你的代码

# no need for the for loop, pull everything back
index = 0
while index< min(len(x), len(y)): # make sure you check for both lengths in case they are not the same
    result = (int(x[index]) + int(y[index]))
    index += 1
    print result # you want to print the result inside the loop so you don't lose it on the next iteration

虽然您可能应该像上面提到的那样使用
zip
,但如果您想继续使用自己的做事方式,您的问题就在这里

当您在python中执行
for
循环时,它是一个foreach,所以当您这样做时

for linha in x:
    print linha
linha
将是
x[n]
的值,其中n是循环的迭代

无论如何,要修复你的代码

# no need for the for loop, pull everything back
index = 0
while index< min(len(x), len(y)): # make sure you check for both lengths in case they are not the same
    result = (int(x[index]) + int(y[index]))
    index += 1
    print result # you want to print the result inside the loop so you don't lose it on the next iteration

看看你是否可以利用。看看你是否可以利用。@MrT他在问题中提到,两个文件的长度相同是的,两个文件的行数相同,所以没有问题。谢谢你的回答@尼森莫毒素肯定是。(y) @MrT他在问题中提到两个文件的长度相同是的,两个文件的行数相同,因此没有问题。谢谢你的回答@尼森莫毒素肯定是。(y)