【python】13_英文词频统计&前 K 个高频元素

1.英文词频统计

作为字典(key-value)的经典应用题目,单词统计几乎出现在每一种语言键值对学习后的必练题目。
主要需求:
写一个函数wordcount统计一篇文章的每个单词出现的次数(词频统计)。统计完成后,对该统计按单词频次进行排序。

from collections import  Counter  #计数排序
text = """
    Enterprise architects will appreciate new capabilities such as lightweight application isolation.
    Application developers will welcome an updated development environment and application-profiling tools. Read more at the Red Hat Developer Blog.
    System administrators will appreciate new management tools and expanded file-system options with improved performance and scalability.

    Deployed on physical hardware, virtual machines, or in the cloud, Red Hat Enterprise Linux 7 delivers the advanced features required for next-generation architectures.
    Where to go from here:

    Red Hat Enterprise Linux 7 Product Page

    The landing page for Red Hat Enterprise Linux 7 information. Learn how to plan, deploy, maintain, and troubleshoot your Red Hat Enterprise Linux 7 system.
    Red Hat Customer Portal

    Your central access point to finding articles, videos, and other Red Hat content, as well as manage your Red Hat support cases.

    Documentation

    Provides documentation related to Red Hat Enterprise Linux and other Red Hat offerings.
    Red Hat Subscription Management

    Web-based administration interface to efficiently manage systems.
    Red Hat Enterprise Linux Product Page

    Provides an entry point to Red Hat Enterprise Linux product offerings.
"""

# # 1. 先拿出字符串里面的所有单词;
words = text.split()    

# 2. 统计每个单词出现的次数
#       1). 如何存储统计好的信息: 字典存储
#       2). 如何处理?
word_count_dict = {}
for word in words:
    if word not in word_count_dict:
        word_count_dict[word] = 1
    else:
        word_count_dict[word]  += 1
print(word_count_dict)
# 3. 排序,获取出现次数最多的单词
counter = Counter(word_count_dict)
print(counter.most_common(7))

在这里插入图片描述
但是在上一篇博文的介绍中,我们知道setdefault( key , value)
方法可以保证使用一个键之前总会将它初始化位一个初始值,同时如果这个键已经存在,调用setdefault没有任何影响。因此,将上述代码可以用这个方法优化。此外,由于print是将信息打印在一行,所以我们可以导入pprint模块。

import pprint
from collections import Counter
words = text.split()

word_dict = {}  #key:word value:count

for item in set(words):
    word_dict.setdefault(item,words.count(item))
    
pprint.pprint(word_dict)

count = Counter(word_dict)
pprint.pprint(count.most_common(5))

在这里插入图片描述

2.前 K 个高频元素: topKFrequent.py

给定一个非空的整数数组,返回其中出现频率前 k 高的元素。

例如,

给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2]。

nums = [1,1,1,2,2,3,3,2,1]
k = int(input('>>'))
results = []

count = Counter(nums)
for item in count.most_common(k):
    results.append(item[0])

print(results)

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值