- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个文件,都有大约 10 行随机数。我想将 file1 中的每一行添加到 file2 中的相应行。我可以在每个文件中只使用一个数字来完成此操作,但有多行数字都在挣扎。例如这是我尝试过的:
file1 = open("file1.txt").read()
file2 = open("file2.txt").read()
result = int(file1) + int(file2)
print(result)
最佳答案
第一步是从文件(程序失败的地方)正确获取数字,然后将它们添加到新列表中。为了制作一个更健壮的程序,我们将捕获一个简单的边缘情况(当两个列表没有相同数量的参数时),作为奖励,您不必对每个文件中的行数进行硬编码:
from itertools import zip_longest
try:
with open("file1") as file1, open("file2") as file2:
numbers1 = [int(line) for line in file1.readlines()]
numbers2 = [int(line) for line in file2.readlines()]
result = [line[0] + line[1] for line in zip_longest(numbers1, numbers2, fillvalue=0)]
except FileNotFoundError:
print("Error opening files")
示例测试:
文件1:
7
1
5
9
7
9
10
3
10
8
文件2:
9
9
8
2
8
5
2
8
9
>>> result
[16, 10, 13, 11, 15, 14, 12, 11, 19, 8]
您必须注意到文件的行数不同,file1(10)、file2(9),我用这个例子来显示您的情况的不确定程度:
both have about 10 lines of random numbers
关于python - 将两个文件中的数字相加(逐行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60944096/
我知道可以创建 object.__mul__(self, other)启用两个对象的自定义乘法的方法。然而,正如 python 文档中所解释的那样, to evaluate the expressio
我无法找到一种好方法来查找列中的所有负条目并将它们向上移动到列中,将它们与现有条目相加(即从当前条目中减去负条目),直到所有值都是正值。 重要的是,最终数据帧没有负值 & 所有以前的负条目 = 0。此
我想将 var string 中的所有元素相加。我确实尝试过这个,但现在我想对每个元素都这样做。 var operator = document.getElementById("operation")
“任何长度的正数表示为数字字符数组,因此介于‘0’和‘9’之间。我们知道最重要的密码位于数组索引 0 的位置。 例子: - 号码是 10282 - 数组将是数字 = [1,0,2,8,2] 考虑到这一
这是我的查询: UPDATE qanda SET amount = amount + 1000 WHERE id = ? AND type = 0; 默认情况下, amount 列为 null,当我执
不同列的值为1时,每列加10分。 要求将累加的结果汇总到表格的一列中。但总分不得超过20分。因此,如果总和达到 30,列中的结果将始终为 20。 SELECT CASE WHE
public static void main(String[] args) { Vector vec = new Vector(); vec.add(new
我正在尝试使用 js 或 jquery 将 +20,2 添加到带有点 9990.95 的总和或带有逗号 9990,95 的总和 9990.95 var price = $( '.sum' ).text
我如何编写一个函数来将二维数组中每一行的内容相加?要添加每一列的内容?我的代码(到目前为止): #include using namespace std; const int QUARTER = 4
这个问题在这里已经有了答案: Count the number of set bits in a 32-bit integer (65 个答案) 关闭 8 年前。 我有这段代码可以获取数字的二进制表
创建了一个 Div,在其中我有 label 元素和 input 元素,我想在每个 div 中获得不同的标签值。如何重用我的 div 组件而不是再次编写相同的代码。 我尝试在 Stackoverflow
所以我在构建代码时遇到了这个问题。这个问题 This work is based on operator overloading, you need to build a string calcula
为了好玩并进一步了解 float 的工作原理,我尝试制作一个函数,它接受两个单精度 float ,并将它们相加。 到目前为止,我所做的对于相同符号的数字非常有效,但当数字具有相反的符号时,它就会分崩离
我想添加两个 map 以及以下行为。 如果键存在 -> 将两个键值相加。 如果键不存在 -> 插入对映射。 我看过一些标准库算法。即转换,但似乎没有做我想要的。 取自此LINK template
我的程序中有两个整数;我们称它们为“a”和“b”。我想将它们加在一起并得到另一个整数。这些是常规的 Python int 对象。我在想;我如何将它们与 Twisted 一起添加?某处是否有特殊的 pe
因此,我必须创建这个程序,从文件中读取多个“工资”,然后将所有工资相加以返回一个“总工资”,并计算所有工资的平均值。 我目前拥有的代码如下: package uploadTask7_countingS
我的结果是这样的: +-------+--------+--------+-----+--------------+ | Count | Equipe | IdTeam | Id | Nam
我有以下数据框: df2 = pd.DataFrame({'season':[1,1,1,2,2,2,3,3],'value' : [-2, 3,1,5,8,6,7,5], 'avail':[3,3,
所以我正在过滤数据(所有字符串),并希望显示以下内容: 第一个字母包含您的搜索的搜索词(例如,在搜索 'Br' 时,我想查看所有以 'Br' 开头的词,例如 Break , 坏了,...) 字符串中包
我试图通过以下关系(仅显示父级)来说明一对多关系的总和: @Entity @Table(name = "Parent") public class Parent implements Serializ
我是一名优秀的程序员,十分优秀!