zoukankan      html  css  js  c++  java
  • Python编程题18--统计字母出现次数并排序

    题目

    给定一个列表,列表元素仅包含字母,请统计每个字母的出现次数,并按出现次数排序,要求最终返回结果为字典形式。

    例如:

    给定一个列表:['a', 'a', 'c', 'b', 'd', 'c', 'c', 'c', 'd', 'd']
    返回结果:{"c": 4, "d": 3, "a": 2, "b": 1}

    实现思路1

    • 利用 Python 里的计数器 Counter ,其可用于追踪值的出现次数,并返回一个 Counter 类对象,是字典 dict 的子类
    • 利用 Python 里的内置函数 sorted() 并结合匿名函数 lambda 进行排序,设置 reverse=True 表示降序
    • 把结果转换为字典 dict 形式返回

    注意:sorted() 返回的结果是一个新的列表list ,这里需要转换为字典格式再返回

    代码实现

    from collections import Counter
    
    def demo(str_list):
        temp = Counter(str_list)
        res_list = sorted(temp.items(), key=lambda x: x[1], reverse=True)
        res_dict = dict(res_list)
        return res_dict
    
    str_list = ["a", "a", "c", "b", "d", "c", "c", "c", "d", "d"]
    print(demo(str_list))
    

    实现思路2

    • 设置1个空字典 temp_dict ,用于存储列表中的字母及其出现次数
    • 遍历列表,如果当前字母不在字典中,那么就将该字母作为键存储到字典,其键值为 1 ;如果当前字母在字典中,那么就让字典中对应的键值加 1
    • 通过字典的 keys()values() 方法得到字母列表 key_list 及对应的字母次数列表 value_list
    • 对字母次数列表 value_list 进行排序,这里用的冒泡排序,在从小到大排序的时候,同时对字母列表 key_list 也进行排序,以保证字母和出现次数相对应
    • 排序后,通过内置函数 zip() ,把2个列表转为字典,并按字母出现次数排序

    代码实现

    def demo(str_list):
        temp_dict = {}
        for i in str_list:
            if i not in temp_dict:
                temp_dict[i] = 1
            else:
                temp_dict[i] += 1
        key_list = list(temp_dict.keys())
        value_list = list(temp_dict.values())
        for i in range(len(value_list) - 1):
            for j in range(len(value_list) - i - 1):
                if value_list[j] > value_list[j + 1]:
                    value_list[j], value_list[j + 1] = value_list[j + 1], value_list[j]
                    key_list[j], key_list[j + 1] = key_list[j + 1], key_list[j]
        res_dict = dict(zip(key_list[::-1], value_list[::-1]))
        return res_dict
    
    str_list = ["a", "a", "c", "b", "d", "c", "c", "c", "d", "d"]
    print(demo(str_list))
    

    实现思路3

    • 设置1个空列表 temp_list ,用于存放字母及其出现次数,其元素通过 元组的方式 (字母, 次数) 来添加
    • 设置一个集合 temp_set ,用于存放列表中的所有字母
    • 对集合进行遍历,遍历的同时把字母及其出现次数添加到 temp_list
    • 对 temp_list 中的元素,按字母出现次数从小到大进行排序
    • 通过内置函数 dict() ,将列表转换为字典,并按字母出现次数排序

    代码实现

    def demo(str_list):
        temp_list = []
        temp_set = set(str_list)
        for i in temp_set:
            temp_list.append((i, str_list.count(i)))
        for i in range(len(temp_list) - 1):
            for j in range(len(temp_list) - i - 1):
                if temp_list[j][1] > temp_list[j + 1][1]:
                    temp_list[j], temp_list[j + 1] = temp_list[j + 1], temp_list[j]
        res_dict = dict(temp_list[::-1])
        return res_dict
    
    str_list = ["a", "a", "c", "b", "d", "c", "c", "c", "d", "d"]
    print(demo(str_list))
    
  • 相关阅读:
    [WCF安全系列]从两种安全模式谈起
    为自定义配置的编辑提供”智能感知”的支持
    在Entity Framework中使用存储过程(二):具有继承关系实体的存储过程如何定义?
    [WCF安全系列]实例演示:TLS/SSL在WCF中的应用[HTTPS]
    [WCF安全系列]谈谈WCF的客户端认证[Windows认证]
    在Entity Framework中使用存储过程(三):逻辑删除的实现与自增长列值返回
    [转] Leaving patterns & practices
    两个简单的扩展方法:TrimPrefix和TrimSuffix
    Oracle 系统表
    让IoC动态解析自定义配置(提供基于Unity的实现)
  • 原文地址:https://www.cnblogs.com/wintest/p/14191646.html
Copyright © 2011-2022 走看看