码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 从collections库的Counter类看items()方法和enumerate()方法


    下面的代码是针对文件的词频统计,使用了collections库及其Counter类
    import collections
    def count_word_frequency(text):
        words = text.lower().split()
        word_counts = collections.Counter(words)
        return word_counts
    def count_fileword_frequency(file_path):
        with open(file_path,'r') as file:
            text = file.read()
        words = text.lower().split()
        word_counts = collections.Counter(words)
        return word_counts
    一个参数是字符串,一个参数是文件路径,一共定义了两个方法

    对变量赋值
    text = "This is a sample text.This text is used to domonstrate word frequency counting."
    file_path = './subtitle.txt'

    输出返回值类型
    word_frequency = count_word_frequency(text)
    print(type(word_frequency))
    word_frequency = count_fileword_frequency('./subtitle.txt')
    print(type(word_frequency))

    输出结果为


    类型是一个类

    如何输出内容,分别用 items() 和 enumerate() 实现
    使用 items()
    for key,value in word_frequency.items():
        print(key,value)
    结果为
    make 2
    sure 1
    you 36
    know 9
    what 11
    you're 7
    supposed 1
    使用enumerate()
    for key,value in enumerate(word_frequency):
        print(key,value,word_frequency[value])
    结果为
    0 make 2
    1 sure 1
    2 you 36
    3 know 9
    4 what 11
    5 you're 7
    6 supposed 1

    如果要求输出前三项,使items()时,需要先转换成list
    for key,value in list(word_frequency.items())[:3]:
        print(key,value)
        
    输出为
    make 2
    sure 1
    you 36
    使用enumerate()时,代码如下
    for key,value in enumerate(word_frequency):
        if key in [0,1,2]:
            print(value,word_frequency[value])
    输出同样为  
    make 2
    sure 1
    you 36      

    从代码中,可以items() 和 enumerate() 都可用可迭代对象的遍历,items()返回元素本身,enumerate()返回迭代对象,有索引和元素组成
        
     

  • 相关阅读:
    tensorflow2 SqueezeNet
    (附源码)springboot毕业生弃置物品交易系统 毕业设计 231151
    ERP管理系统的运作流程是怎样的?
    python——基础排坑+经验总结(持续更新)
    不知道图片加文字水印怎么弄?这3个方法自媒体达人必学
    2.3.1 交换机的链路聚合技术
    SpringMVC 解析(三) Controller 注解
    力扣刷题day48|583两个字符串的删除操作、72编辑距离
    计算机毕业设计ssm大学生比赛信息管理系统38iiq系统+程序+源码+lw+远程部署
    【MySQL】索引的作用及知识储备
  • 原文地址:https://blog.csdn.net/lepton126/article/details/132919413
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号