需要从文件中读取并添加元素,然后在python 3.4中获得avg



我有一个输入文件如下:

75647485 10 20 13 12 14 17 13 16
63338495 15 20 11 17 18 20 17 20
00453621  3 10  4 10 20 18 15 10
90812341 18 18 16 20  8 20  7 15

我需要找到从第二个元素开始到结束[1:8]的每一行的平均值,并给出输出为:

ID        Mean     Lowest number       Highest number   
75647485  14.37     10                     20
90812341  ...       ...                    ...

我是蟒蛇的新手,所以有人能帮忙吗。我不需要将输出写入文件,但只需在控制台上显示即可。

感谢

array = [ [int(s) for s in line.split()] for line in open('file') ]
for line in array:
    print('%08i %3.1f %3i %3i' % (line[0], sum(line[1:])/len(line[1:]), min(line[1:]), max(line[1:])))

这会产生输出:

75647485 14.4  10  20
63338495 17.2  11  20
00453621 11.2   3  20
90812341 15.2   7  20

备用版本

为了确保文件句柄正确关闭,此版本使用with。此外,字符串格式化是用更现代的format函数完成的:

with open('file') as f:
    array = [ [int(s) for s in line.split()] for line in f ]
for line in array:
    print('{:08.0f} {:3.1f} {:3.0f} {:3.0f}'.format(line[0], sum(line[1:])/len(line[1:]), min(line[1:]), max(line[1:])))

您可以使用numpy:

import numpy
numpy.mean(mylist[1:8])
fileRecord = namedtuple('RecordID', 'num1, num2, num3, num4, num5, num6, num7, num8)
import csv
for line in csv.reader(open("file.txt", header=None, delimiter=r"s+")):
    numList = fileRecord._make(line)
    numListDict = numList._asdict()
    lowest = numListDict[0]
    highest = numListDict[7]
    for (key, value) in numListDict: 
       total += value;
    mean = total/8
    print (lowest, highest, mean)

我建议使用panda。更具可扩展性和更多功能。它也是基于numpy。

import pandas as pd
x='''75647485 10 20 13 12 14 17 13 16
63338495 15 20 11 17 18 20 17 20
00453621  3 10  4 10 20 18 15 10
90812341 18 18 16 20  8 20  7 15'''
from cStringIO import StringIO # py27
df = pd.read_csv(StringIO(x), delim_whitespace=True, header=None, index_col=0)
print df.T.max()
#75647485    20
#63338495    20
#453621      20
#90812341    20
print df.T.min()
#75647485    10
#63338495    11
#453621       3
#90812341     7
print df.T.mean()
#75647485    14.375
#63338495    17.250
#453621      11.250
#90812341    15.250

相关内容

最新更新