如何让它在同一行打印输出?
How do I make it print the outputs on same line?
我正在尝试打开两个文件并收集 2010 年至 2019 年的信息,并仅报告当年索赔的均值和标准差
mean_file = open('data/mean.txt', 'r')
std_file = open('data/std.txt', 'r')
count = 2009
for line in std_file:
std = float(line)
for line in mean_file:
mean = float(line)
count += 1
print('Year',count, 'Mean:', mean, 'Standard Deviation:', std)
mean_file.close()
std_file.close()
我有上面的代码。我得到的输出是
Year 2019 Mean: 217557.4 Standard Deviation: 82296.33
Year 2019 Mean: 217557.4 Standard Deviation: 77808.0
Year 2019 Mean: 217557.4 Standard Deviation: 66939.77
Year 2019 Mean: 217557.4 Standard Deviation: 65486.56
Year 2019 Mean: 217557.4 Standard Deviation: 59126.12
Year 2019 Mean: 217557.4 Standard Deviation: 58712.14
Year 2019 Mean: 217557.4 Standard Deviation: 55465.54
Year 2019 Mean: 217557.4 Standard Deviation: 44621.54
Year 2019 Mean: 217557.4 Standard Deviation: 47821.1
Year 2019 Mean: 217557.4 Standard Deviation: 43170.7
每次我改变缩进的位置,它都会给出不同的答案。我希望平均值和标准偏差同时打印在同一行上,就像下面的输出一样。
我希望输出如下所示。我怎样才能让它像下面的输出一样打印出来?
Year 2010 Mean: 455692.98 Standard Deviation: 82296.33
Year 2011 Mean: 409110.4 Standard Deviation: 77808.0
Year 2012 Mean: 372226.67 Standard Deviation: 66939.77
Year 2013 Mean: 341826.79 Standard Deviation: 65486.56
Year 2014 Mean: 306567.67 Standard Deviation: 59126.12
Year 2015 Mean: 276956.5 Standard Deviation: 58712.14
Year 2016 Mean: 263900.21 Standard Deviation: 55465.54
Year 2017 Mean: 243116.25 Standard Deviation: 44621.54
Year 2018 Mean: 220894.98 Standard Deviation: 47821.1
Year 2019 Mean: 217557.4 Standard Deviation: 43170.7
编辑:抱歉,弄错了。我知道你是从哪里来的。除了缩进之外,以下所有要点仍然适用。要修复此错误,您应该使用 zip
,它允许您一次迭代两个文件:
with open('std.txt', 'r') as std_file:
with open('means.txt', 'r') as mean_file:
for line in zip(std_file,mean_file):
std = float(line[0])
mean = float(line[1])
count += 1
print('Year',count, 'Mean:', mean, 'Standard Deviation:', std)
这给了我们:
Year 2010 Mean: 455692.98 Standard Deviation: 82296.33
Year 2011 Mean: 409110.4 Standard Deviation: 77808.0
Year 2012 Mean: 372226.67 Standard Deviation: 66939.77
Year 2013 Mean: 341826.79 Standard Deviation: 65486.56
Year 2014 Mean: 306567.67 Standard Deviation: 59126.12
Year 2015 Mean: 276956.5 Standard Deviation: 58712.14
Year 2016 Mean: 263900.21 Standard Deviation: 55465.54
Year 2017 Mean: 243116.25 Standard Deviation: 44621.54
Year 2018 Mean: 220894.98 Standard Deviation: 47821.1
Year 2019 Mean: 217557.4 Standard Deviation: 43170.7
一些可使您的代码更易于阅读并因此更易于调试的快速提示:使用上下文管理器(即 with
语句),并且不要在嵌套 for-loops 中重复使用迭代变量.
使用上下文管理器自动打开和关闭文件,通常更安全。你可以这样做:
with open('data/std.txt', 'r') as std_file:
with open('data/mean.txt', 'r') as mean_file:
#do stuff
此外,在for-loops中重复使用line
不仅是不好的做法,而且将不可避免地导致程序重写你仍然想要的变量。 最后,您的缩进是错误的:您只是在遍历所有均值文件后才打印,因此将始终获得相同的均值输出。 将所有这些放在一起,我们有。
count = 2009
with open('data/std.txt', 'r') as std_file:
with open('data/mean.txt', 'r') as mean_file:
for line_std in std_file:
std = float(line_std)
for line_mean in mean_file:
mean = float(line_mean)
count += 1
print('Year',count, 'Mean:', mean, 'Standard Deviation:', std)
应该可以按预期工作。 请参见上面的固定代码
for sline, mline in zip(std_file, mean_file):
std = float(line)
mean = float(line)
count = count+1
print('Year',count, 'Mean:', mean, 'Std:', std)
我正在尝试打开两个文件并收集 2010 年至 2019 年的信息,并仅报告当年索赔的均值和标准差
mean_file = open('data/mean.txt', 'r')
std_file = open('data/std.txt', 'r')
count = 2009
for line in std_file:
std = float(line)
for line in mean_file:
mean = float(line)
count += 1
print('Year',count, 'Mean:', mean, 'Standard Deviation:', std)
mean_file.close()
std_file.close()
我有上面的代码。我得到的输出是
Year 2019 Mean: 217557.4 Standard Deviation: 82296.33
Year 2019 Mean: 217557.4 Standard Deviation: 77808.0
Year 2019 Mean: 217557.4 Standard Deviation: 66939.77
Year 2019 Mean: 217557.4 Standard Deviation: 65486.56
Year 2019 Mean: 217557.4 Standard Deviation: 59126.12
Year 2019 Mean: 217557.4 Standard Deviation: 58712.14
Year 2019 Mean: 217557.4 Standard Deviation: 55465.54
Year 2019 Mean: 217557.4 Standard Deviation: 44621.54
Year 2019 Mean: 217557.4 Standard Deviation: 47821.1
Year 2019 Mean: 217557.4 Standard Deviation: 43170.7
每次我改变缩进的位置,它都会给出不同的答案。我希望平均值和标准偏差同时打印在同一行上,就像下面的输出一样。 我希望输出如下所示。我怎样才能让它像下面的输出一样打印出来?
Year 2010 Mean: 455692.98 Standard Deviation: 82296.33
Year 2011 Mean: 409110.4 Standard Deviation: 77808.0
Year 2012 Mean: 372226.67 Standard Deviation: 66939.77
Year 2013 Mean: 341826.79 Standard Deviation: 65486.56
Year 2014 Mean: 306567.67 Standard Deviation: 59126.12
Year 2015 Mean: 276956.5 Standard Deviation: 58712.14
Year 2016 Mean: 263900.21 Standard Deviation: 55465.54
Year 2017 Mean: 243116.25 Standard Deviation: 44621.54
Year 2018 Mean: 220894.98 Standard Deviation: 47821.1
Year 2019 Mean: 217557.4 Standard Deviation: 43170.7
编辑:抱歉,弄错了。我知道你是从哪里来的。除了缩进之外,以下所有要点仍然适用。要修复此错误,您应该使用 zip
,它允许您一次迭代两个文件:
with open('std.txt', 'r') as std_file:
with open('means.txt', 'r') as mean_file:
for line in zip(std_file,mean_file):
std = float(line[0])
mean = float(line[1])
count += 1
print('Year',count, 'Mean:', mean, 'Standard Deviation:', std)
这给了我们:
Year 2010 Mean: 455692.98 Standard Deviation: 82296.33
Year 2011 Mean: 409110.4 Standard Deviation: 77808.0
Year 2012 Mean: 372226.67 Standard Deviation: 66939.77
Year 2013 Mean: 341826.79 Standard Deviation: 65486.56
Year 2014 Mean: 306567.67 Standard Deviation: 59126.12
Year 2015 Mean: 276956.5 Standard Deviation: 58712.14
Year 2016 Mean: 263900.21 Standard Deviation: 55465.54
Year 2017 Mean: 243116.25 Standard Deviation: 44621.54
Year 2018 Mean: 220894.98 Standard Deviation: 47821.1
Year 2019 Mean: 217557.4 Standard Deviation: 43170.7
一些可使您的代码更易于阅读并因此更易于调试的快速提示:使用上下文管理器(即 with
语句),并且不要在嵌套 for-loops 中重复使用迭代变量.
使用上下文管理器自动打开和关闭文件,通常更安全。你可以这样做:
with open('data/std.txt', 'r') as std_file:
with open('data/mean.txt', 'r') as mean_file:
#do stuff
此外,在for-loops中重复使用line
不仅是不好的做法,而且将不可避免地导致程序重写你仍然想要的变量。 最后,您的缩进是错误的:您只是在遍历所有均值文件后才打印,因此将始终获得相同的均值输出。 将所有这些放在一起,我们有。
count = 2009
with open('data/std.txt', 'r') as std_file:
with open('data/mean.txt', 'r') as mean_file:
for line_std in std_file:
std = float(line_std)
for line_mean in mean_file:
mean = float(line_mean)
count += 1
print('Year',count, 'Mean:', mean, 'Standard Deviation:', std)
应该可以按预期工作。 请参见上面的固定代码
for sline, mline in zip(std_file, mean_file):
std = float(line)
mean = float(line)
count = count+1
print('Year',count, 'Mean:', mean, 'Std:', std)