【Python字典分析文本数据】

Python字典分析文本数据

本文作为经验分享,适合python初学者提高对基本数据类型的使用技巧,如果你是一个python大佬,也也也可以看!!!

目标

读完全文,你至少可以获得使用python的这些经验:

  1. 读取txt文本文件并进行分析处理;
  2. 掌握字符串转列表的方法;
  3. 掌握一种循环结构;
  4. 掌握字典的巧妙使用方法

实例:按类别计算文件中水果的平均重量

现有一份描述水果种类、颜色、重量的数据,存放在data.txt文件中,希望我们能够使用python做一些简单的数据分析,这里就不妨简单的按水果类别计算一下各类水果的平均重量。(要求读入数据,能够保证文件内容的拓展与变更!)

data.txt
category  color weight
apple  red  5
banana  yellow  6
orange  orange  7
apple  red  9
banana  yellow  6
orange  orange  8
apple  red  10
banana  yellow  6
orange  orange  4

直接上代码

import os

# open the txt file and name it as f
with open("data.txt") as f:
    title = f.readline()
    # print the title of the data
    #print("The title of file : "+ str(title.rstrip()))
    
    fruit_total_weight = {}
    count = {}
    while(True):
        line = f.readline()
        if (line == ""):
            break
        
        # split the line with two whitespace
        str_list = []
        str_list = line.rstrip().split("  ")
        
        # use dicts to storage total weight and number
        if (str_list[0] in fruit_total_weight):
            fruit_total_weight[str_list[0]] += float(str_list[2])
            count[str_list[0]] += 1
        else:
            fruit_total_weight[str_list[0]] = float(str_list[2])
            count[str_list[0]] = 1
    # close file
    f.close()
    

    
# calculate and print average weight
fruit_ave_weight = {}
for fruit in fruit_total_weight.keys():
    fruit_ave_weight[fruit] = fruit_total_weight[fruit] / count[fruit]
    print("The average weight of " + str(fruit) + " is {:.2f}".format(fruit_ave_weight[fruit]))

结果分析

第一次执行代码

按照水果的种类,分别输出了各种水果的平均重量

[Running] python -u "c:\Users\qinmm\Desktop\demo\calculateAve.py"
The average weight of apple is 8.00
The average weight of banana is 6.00
The average weight of orange is 6.33

[Done] exited with code=0 in 0.082 seconds

第二次执行代码

细心的已经发现上面的数据三种水果各有三行,能算出结果会不会有巧合呢?下面我们对数据做一点点的修改,在原数据的末尾加入下面几行,这样三种水果的数量各不相同分别为4、5、6,那么还能不能正确计算呢?

banana  yellow  7
orange  orange  8
orange  orange  2

再次执行代码,输出结果如下,发现虽然3种水果数量不同,但是我们依然能够正确计算:

[Running] python -u "c:\Users\qinmm\Desktop\demo\calculateAve.py"
The average weight of apple is 8.00
The average weight of banana is 6.25
The average weight of orange is 5.80

[Done] exited with code=0 in 0.07 seconds

第三次执行代码

那么问题叒叒叒来了,只能计算这三种水果嘛?我们再增加一些数据来看一看。

grape  purple  4
grape  purple  7
peach  pink  8
peach  pink  2

再次执行代码,输出结果如下,发现虽然又多了两种水果,但是我们仍然还是能够正确计算:

[Running] python -u "c:\Users\qinmm\Desktop\demo\calculateAve.py"
The average weight of apple is 8.00
The average weight of banana is 6.25
The average weight of orange is 5.80
The average weight of grape is 5.50
The average weight of peach is 5.00

[Done] exited with code=0 in 0.069 seconds

都说到这里了,你还不一键三连嘛(手动WinK!)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值