前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Python获取词频中排名第二的词汇

使用Python获取词频中排名第二的词汇

作者头像
前端皮皮
发布2022-08-17 19:01:11
3920
发布2022-08-17 19:01:11
举报
文章被收录于专栏:前端进阶学习交流

大家好,我是皮皮。

一、前言

前几天在Python最强王者交流群【Chloe】问了一道Python处理的问题,如下图所示。

原始数据如下:

代码语言:javascript
复制
f('Both of these issues are fixed by postponing the evaluation of annotations. Instead of compiling code which executes expressions in annotations at their definition time, the compiler stores the annotation in a string form equivalent to the AST of the expression in question. If needed, annotations can be resolved at runtime using typing.get_type_hints(). In the common case where this is not required, the annotations are cheaper to store (since short strings are interned by the interpreter) and make startup time faster.')

怎么按照values排序? 并且取倒数第二个值?

二、实现过程

这里大家给出一个思路,如下所示:

下次遇到这种词频的需求,都可以考虑使用Counter来实现,事半功倍。

这里【月神】给了一份示例代码,如下所示:

代码语言:javascript
复制
from collections import Counter

c = Counter('lost lost an apple apple '.split())

c.most_common(2).pop()[0]

运行之后,结果如下图所示:

后来【瑜亮老师】给了一份具体的代码,如下所示:

代码语言:javascript
复制
from collections import Counter
ss = 'Both of these issues are fixed by postponing the evaluation of annotations. Instead of compiling code which executes expressions in annotations at their definition time, the compiler stores the annotation in a string form equivalent to the AST of the expression in question. If needed, annotations can be resolved at runtime using typing.get_type_hints(). In the common case where this is not required, the annotations are cheaper to store (since short strings are interned by the interpreter) and make startup time faster.'
data = Counter(ss.split())
print(data)
result = data.most_common(2)[1][0]
print(result)

这个most_common()函数完美地解决了粉丝的问题!

后来【Chloe】自己也提供了一个方法,也是可行的,条条大路通罗马。

代码语言:javascript
复制
def f(s):
    d = {}
    for i in s.split():
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    # print(d)
    return d


result = f('Both of these issues are fixed by postponing the evaluation of annotations. Instead of compiling code which executes expressions in annotations at their definition time, the compiler stores the annotation in a string form equivalent to the AST of the expression in question. If needed, annotations can be resolved at runtime using typing.get_type_hints(). In the common case where this is not required, the annotations are cheaper to store (since short strings are interned by the interpreter) and make startup time faster.')

print(sorted(zip(result.values(), result.keys()), reverse=True)[1])
for data in sorted(zip(result.values(), result.keys()), reverse=True):
    print(data)

运行结果如下图所示:

她还提供了另外一个方法,如下所示:

代码语言:javascript
复制
def f(s):
    d = {}
    for i in s.split():
        if i in d:
            d[i] += 1
        else:
            d[i] = 1

    d = {v: k for k, v in d.items()}
    print(d.items())
    d = sorted(d.items())
    print(d)
    print(d[-2])


f(
    'Both of these issues are fixed by postponing the evaluation of annotations. Instead of compiling code which executes expressions in annotations at their definition time, the compiler stores the annotation in a string form equivalent to the AST of the expression in question. If needed, annotations can be resolved at runtime using typing.get_type_hints(). In the common case where this is not required, the annotations are cheaper to store (since short strings are interned by the interpreter) and make startup time faster.')

运行结果如下图所示:

三、总结

大家好,我是皮皮。这篇文章主要盘点了一道使用Python处理数据的问题,文中针对该问题给出了具体的解析和代码实现,帮助粉丝顺利解决了问题。

最后感谢粉丝【Chloe】提问,感谢【月神】、【瑜亮老师】给出的思路和代码解析,感谢【dcpeng】、【冯诚】、【老松鼠】等人参与学习交流。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-07-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python共享之家 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
  • 二、实现过程
  • 三、总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档