weixin_38558294
hhhhh耗
采纳率14.3%
2018-05-19 05:21 阅读 4.5k

Python疑问,求平均值问题

40

105.408635, 109.058062
109.711955, 107.612010
106.770910, 107.178111
104.902315, 108.942544
106.429432, 109.986264
106.350715, 108.745940
107.208566, 106.724891
105.758081, 107.954024
106.635802, 105.702770
109.028467, 105.940024
104.741757, 109.545083
104.469050, 105.768578
105.860169, 106.943419
104.190133, 108.945727
108.427905, 107.905775
105.105144, 108.668921
105.584954, 110.006551
105.130636, 105.577014
106.174433, 106.636557
107.386285, 108.500267
107.742580, 110.253006
107.594896, 106.710349
109.138761, 106.629520
...... .......
Python读取txt文件,然后咋将第一列数字求平均值啊

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2018-05-19 07:40
     import sys
    
    sum = 0
    cnt = 0
    f = open('1.txt', 'r')
    s = f.readline()
    while (s):
        sum = sum + float(s.split(",")[0])
        cnt = cnt + 1
        s = f.readline()
    print(sum / cnt)
    f.close()
    
    
    106.51093830434785
    
    点赞 3 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2018-05-19 07:42

    图片说明

    这是运行效果

    如果满意,麻烦点个采纳,谢谢。

    点赞 6 评论 复制链接分享
  • qq_39075717 qq_39075717 2018-05-19 16:26

    import pandas as pd

    a=pd.read_csv('mean.txt')
    mean = a['col1'].mean()
    print mean

    点赞 1 评论 复制链接分享
  • sinat_37275759 mealy_moore 2018-05-20 09:18

    import sys
    sum = 0
    cnt = 0
    f = open('1.txt', 'r')
    files = f.readline()
    while (files ):
    sum = sum + float(files .split(",")[0])
    cnt = cnt + 1
    files = f.readline()
    print(sum / cnt)
    f.close()

    点赞 评论 复制链接分享

相关推荐