打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

回复“资源”即可获赠Python学习资料

应共冤魂语,投诗赠汨罗。

大家好,我是皮皮。

一、前言

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

原始数据如下:

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来实现,事半功倍。

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

from collections import Counter

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

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

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

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

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】自己也提供了一个方法,也是可行的,条条大路通罗马。

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)

运行结果如下图所示:

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

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】、【冯诚】、【老松鼠】等人参与学习交流。

------------------- End -------------------

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Learn Python in 10 Minutes
Python函数后面的箭头
Re: wht is jit compiler
在Python2.7下如何安装TA
python函数注释 参数后面加冒号: 函数后面的箭头
Systems Programming
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服