简体   繁体   English

如何让它在同一行打印输出?

[英]How do I make it print the outputs on same line?

I am trying to open two files and collect information for the years 2010 to 2019 and report the mean and standard deviation of just the claims in that year我正在尝试打开两个文件并收集 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()

I have the code above.我有上面的代码。 The output I got is我得到的output是

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

Each time I change the position of the indent, it gives a different answer.每次我更改缩进的 position 时,它都会给出不同的答案。 I want the mean and standard deviation to be printed on the same line simultaneously just like the output below.我希望平均值和标准偏差同时打印在同一行上,就像下面的 output 一样。 I want the output to be like below.我希望 output 如下所示。 How do I make it print exactly like the output below?我如何让它像下面的 output 一样打印出来?

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

EDIT: Apologies, made a mistake.编辑:抱歉,犯了一个错误。 I see where you are coming from.我知道你是从哪里来的。 All the points below, other than indentation, still apply.除了缩进之外,以下所有要点仍然适用。 To fix this error, you should use zip , which allows you to iterate both files at once:要修复此错误,您应该使用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)

Which gives us:这给了我们:

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

Some quick pointers that will make your code easier to read, and thus easier to debug: Use context managers (ie with statements), and don't reuse iteration variables in nested for-loops.一些可以使您的代码更易于阅读并因此更易于调试的快速提示:使用上下文管理器(即with语句),并且不要在嵌套的 for 循环中重复使用迭代变量。

Using a context manager automatically opens and closes a file, and is generally safer.使用上下文管理器自动打开和关闭文件,通常更安全。 You can do it like so:你可以这样做:

with open('data/std.txt', 'r') as std_file:
    with open('data/mean.txt', 'r') as mean_file:
        #do stuff

Additionally, reusing line in both for-loops is not only bad practice, but will inevitably cause the program to rewrite variables you still want.此外,在两个 for 循环中重复使用line不仅是不好的做法,而且将不可避免地导致程序重写您仍然需要的变量。 Finally, your indentation is wrong: you are only printing after iteration through ALL of the mean file, and as such will always get the same output for mean.最后,你的缩进是错误的:你只是在迭代所有的平均文件后打印,因此对于平均来说总是会得到相同的 output。 Putting this all together, we have.把这一切放在一起,我们有。

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)

Which should work as intended.哪个应该按预期工作。 SEE ABOVE FOR FIXED CODE参见上面的固定代码

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM