Python学习_任务四

学习内容

  1. 实现一个函数get_max_i_word(filename,i),返回文件(由datawhale提供)中最多的几个频次的字母的列表,并将i设为5,打印出来字母的列表组成的字符串。
def get_max_i_word(filename,i):
    with open(filename) as file_object:
        lines = file_object.readlines()  # 存储于列表
        counts = {}                      #用于统计字母数目的空字典
        #统计文件各个字母的数目
        for line in lines:
            if line not in counts:
                counts[line] = 1
            else:
                counts[line] += 1
        print("字母的个数列表:")
        print(counts)

        words = []                       #用于存储频数最高的i个字母的空列表
        #找出频数最高的i个字母并写入列表
        for i_max in range(i):
            v0 = 0
            k0 = ''
            for k,v in counts.items():
                if v >= v0:
                    k0 = k
                    v0 = v
            words.append(k0)            #将频数最高的字母写入列表中
            del counts[k0]             #删除字典中已统计的字母
        print("打印频数最高的i个字母:")
        print(words)

        #将打印出来的字母列表组成字符串
        pi_string = ''
        for word in words:
            pi_string += word.strip()
        print("字符串形式:")
        print(pi_string.title())

filename = 'data.txt'
i = 5
get_max_i_word(filename,i)

结果:

字母的个数列表:
{'n\n': 44, 'e\n': 96, 'o\n': 42, 'w\n': 100, 'l\n': 97, 'u\n': 57, 'b\n': 55, 'x\n': 53, 'f\n': 40, 'r\n': 50, 'p\n': 59, 'z\n': 46, 'd\n': 49, 't\n': 56, 'y\n': 50, 'a\n': 98, 'j\n': 45, 'm\n': 47, 'h\n': 99, 'g\n': 54, 'i\n': 49, 'k\n': 33, 'c\n': 45, 'v\n': 47, 's\n': 38, 'q\n': 41}
打印频数最高的i个字母:
['w\n', 'h\n', 'a\n', 'l\n', 'e\n']
字符串形式:
Whale
  1. 开放题:按你自己的想法设计一个类或多个类,设计继承关系,可以只给出抽象的结构,但必须是实际问题的抽象,并解释自己为什么这么设计,最好能有拓展性和实现了部分核心代码。可以参考github上的python项目
class Restaurant():
    """一次模拟餐馆的简单尝试"""

    def __init__(self, restaurant_name, cuisine_type):
        """初始化属性restaurant属性"""
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_serverd = 0

    def describe_restaurant(self):
        """描述restaurant"""
        print("\nThe name of the restaurant is "+ self.restaurant_name.title() + ".")
        print("Cuisine type is "+ self.cuisine_type + ".")

    def open_restaurant(self):
        """显示正在营业"""
        print(self.restaurant_name.title() + " is in operation.")

    def set_number_serverd(self,number_serverd):
        """设置多少人在餐馆就过餐"""
        self.number_serverd = number_serverd
        print("The number of people dining at the restaurant is " + str(self.number_serverd) + " people.")

    def increment_number_served(self,inc_number):
        """设置每日递增就餐人数"""
        self.number_serverd += inc_number
        print("The number of people dining in the restaurant has increased to " +
              str(self.number_serverd) + " .")

class IceCreamStand(Restaurant):
    """继承Restaurant的冰淇淋店"""

    def __init__(self, restaurant_name, cuisine_type):
        """初始化父类的属性"""
        super().__init__(restaurant_name, cuisine_type)
        #用于存储冰淇淋口味的列表
        self.flavors = ['A','B','C']

    def print_flavors(self):
        print("Ice cream flavor: ")
        print(self.flavors)

restaurant = Restaurant("happy restaurant","Hunan cuisine")
restaurant.describe_restaurant()
restaurant.open_restaurant()
restaurant.set_number_serverd(100)
restaurant.increment_number_served(20)

ice = IceCreamStand("good to eat","sweet taste")
ice.describe_restaurant()
ice.print_flavors()

结果:

The name of the restaurant is Happy Restaurant.
Cuisine type is Hunan cuisine.
Happy Restaurant is in operation.
The number of people dining at the restaurant is 100 people.
The number of people dining in the restaurant has increased to 120 .

The name of the restaurant is Good To Eat.
Cuisine type is sweet taste.
Ice cream flavor: 
['A', 'B', 'C']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值