Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
从文本文件读取数据时,必须使用append-Python_Python - Fatal编程技术网

从文本文件读取数据时,必须使用append-Python

从文本文件读取数据时,必须使用append-Python,python,Python,我必须创建一个程序来读取一个长度为电路板长度的文本文件。我必须找到并打印这些长度的平均值。然后我必须打印一张长度表,以及它们与平均值的差异。然后我需要计算有多少差值大于0.10 到目前为止,我已经能够找到板的平均值,并且能够计算出大于0.10的差异量。但我不知道如何将长度和差异添加到列表中,以便能够打印每个长度旁边的差异 代码: 输出: average: 7.97 Number of boards > 0.10 from average:3 我不知道如何打印长度和差异表 Length

我必须创建一个程序来读取一个长度为电路板长度的文本文件。我必须找到并打印这些长度的平均值。然后我必须打印一张长度表,以及它们与平均值的差异。然后我需要计算有多少差值大于0.10

到目前为止,我已经能够找到板的平均值,并且能够计算出大于0.10的差异量。但我不知道如何将长度和差异添加到列表中,以便能够打印每个长度旁边的差异

代码:

输出:

average: 7.97
Number of boards > 0.10 from average:3
我不知道如何打印长度和差异表

Length    Difference
#         #
#         #
# and so on
differences = [l - mean_board_length for l in board_lengths]

你的计算顺序不对。在知道输入的平均值之前,不能做任何区别。目前,第一块板无法标记,因为此时的
avg
只不过是第一块板的长度

您需要分步骤处理此问题:

  • 将所有数据读入一个列表
  • 计算平均数
  • 回顾列表,对照平均值检查每个项目
  • 在第三步中,您应该可以轻松地生成输出


    你能从这里开始吗?

    我认为当你试图打印它时,你的长度变量没有被实例化。 您可以创建一个元组数组来保存
    length
    s和
    dif
    s:

    
    对于fo中的行:
    板。附加((长度,dif)) 对于板中的项目: 打印(项目)


    这样,你在阅读文本文件后就可以得到它们。

    普劳恩的答案是正确的,我认为你没有正确计算平均值

    我认为你应该首先遍历所有的电路板,并将它们的长度存储在一个列表中

    board_lengths = []
    with open("boards.txt", "r") as fo:
        for line in fo:
            board_lengths.append(float(line))
    
    完成此操作后,您可以总结列表以创建您的意思

    import numpy as np
    mean_board_length = np.mean(board_lengths)
    
    你可以找到不同之处

    Length    Difference
    #         #
    #         #
    # and so on
    
    differences = [l - mean_board_length for l in board_lengths]
    
    以及差值大于0.1时的次数

    times = np.sum([d>0.1 for d in differences])