博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字典排序,初始化,简单使用
阅读量:4571 次
发布时间:2019-06-08

本文共 861 字,大约阅读时间需要 2 分钟。

 

想对字典wordhash排序:

通过list实现:wordlist

wordhash={'\ufeff': 1, '作者': 8, ':': 114, 'Calvin': 1, 'Shi': 1, '链接': 1, 'www': 1, '.': 35, 'zhihu': 1, 'question': 1, '27068465': 1}wordlist = [('\ufeff', 1), ('Calvin', 1), ('Shi', 1), ('链接', 1), ('www', 1), ('zhihu', 1), ('question', 1), ('27068465', 1)]
item:item[1]代表是元组('Calvin', 1)中的第二个元素‘1’进行排序
reverse=False代表词频从小到大
wordhash = {}   #字典的初始化    wordhash = cipin(content)    wordlist = sorted(wordhash.items(),key=lambda item:item[1],reverse=False)     for tuple in wordlist:        word = tuple[0]        print(word+'    '+str(wordhash[word]))

 

判断某个key是否在字典中

 

def cipin(content):    wordhash = {}    words = content.split()    for word in words:        if word in wordhash:            wordhash[word] += 1        else:            wordhash[word] = 1    return wordhash

 

转载于:https://www.cnblogs.com/hozhangel/p/9966391.html

你可能感兴趣的文章
微信小程序之转发功能(附效果图和源码)
查看>>
成熟法则
查看>>
Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
查看>>
[转]SP 2010: How To – Event Receivers and Custom Error Pages
查看>>
棋牌分布式架构
查看>>
【安卓基础】倒计时按钮封装(验证码倒计时按钮)
查看>>
configparser模块
查看>>
Crack的必备工具(2)
查看>>
SelectQueryBuilder的用法
查看>>
无法启动此程序,因为计算机丢失MSVCP120.dll
查看>>
jQuery EasyUI API 中文文档 - 表单(Form)
查看>>
笔试遇到过的算法题
查看>>
android 强制设置横屏 判断是横屏还是竖屏
查看>>
几种颜色模型的转换公式
查看>>
Oracle SQL多表查询
查看>>
控制字段的权限设计(2) --数据库设计
查看>>
我在都匀做网优(补)
查看>>
深入探究单元测试编写
查看>>
【读书笔记】你不知道的JavaScript(上卷)--作用域是什么
查看>>
Caffe初试(二)windows下的cafee训练和测试mnist数据集
查看>>