且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

numpy或scipy入门?

更新时间:2023-02-26 16:28:53

这是一种面向对象的语言,它将对python的工作方式有一些基本的了解.因此,您基本上可以解析输入文件, 由于您说文件只有一列,因此只需要将值转换为数字

It will take some basic understanding of how python works, it is an object oriented language. So you basically parse the input file, Since you say the file has only one column, you just need to convert the values to numbers

with open('stats1.txt') as f:
    lines=[float(i) for i in f.xreadlines()] 

这将为您提供一张表格,类似于使用电子表格打开该表格时所看到的表格,请注意,该表格假定所有值都是数字,如果您混入了文字,则将需要更多排序.然后将其变成一个numpy数组

That will get you a table similar to what you'd see if you opened it with a spreadsheet, note that it assumes all values are numbers, if you have text mixed in, it will take some more sorting. Then you turn that into a numpy array

nlines=numpy.array(lines)

然后,获取您的统计值,

Then, to get your statistical values,

stdev=nlines.std()
min_=nlines.min()
max_=nlines.max()
mean=nlines.mean()
import collections
mode=collections.Counter(nlines).most_common()[0]