所以我想创建一个列表,其中包含一个数字列表的运行总数,但是我仍然希望显示第一个数字而不添加任何内容,有点像这样:
输入
0.1
0.3
0.2
0.4
0.2
期望输出
0.1
0.4
0.6
1.0
1.2
输出我
0.4
0.6
1.0
1.2
以下是我正在使用的代码:
open('widthdepth1.txt','w').writelines([ line for line in open("Test1.txt") if "WIDTH" in line or "DEPTH" in line])
for line in open("widthdepth1.txt"):
line = line.strip("")
parts = line.split(":")
category = parts[0]
value = parts[1]
with open("widthdepth1.txt") as f:
width = [line.split(":")[1] for line in f if "WIDTH" in line]
widthtotal = float(width[0])
h = open("WidthValue1.txt", "w")
for line in width[1:19]:
widthtotal += float(line)
h.write(str("%0.4f" %widthtotal)+"\n")
print ("%0.4f" %widthtotal)
我是python的新手,所以非常感谢任何帮助。
答案 0 :(得分:1)
widthtotal = float(width[0])
h = open("WidthValue1.txt", "w")
for line in width[1:19]:
widthtotal += float(line)
在循环开始之前, widthtotal
已经包含0.1,并且在打印任何内容之前会0.3
添加0.1
。所以widthtotal
没有打印就不足为奇了。尝试将width
初始化为零,然后迭代完整的widthtotal = 0
h = open("WidthValue1.txt", "w")
for line in width:
widthtotal += float(line)
集合。
$CheckNow = !empty(array_intersect($Main, $Search));
if ($CheckNow) {
# code...
}