python---词频统计

# 词频统计
mstr = "Enjoy that uniquenesss1. You do not have to pretend in order to seem more like someone else. You do not have to lie to hide the parts of you that are not like what you see in anyone else.You were meant to be different. Nowhere, in all of history, will the same things be going on in anyone’s mind, soul and spirit as are going on in yours right now.If you did not exist, there would be a hole in creation, a gap2 in history, and something missing from the plan for humankind. Treasure your uniqueness. It is a gift given only to you. Enjoy it and share it!"

# 把标点符号替换了
nstr = mstr.replace("!", "").replace(".", "").replace(",", "").replace("?", "")

# 以空格分割单词
mlist = nstr.split() # 默认以空格

# set集合去重
mset = set(mlist)

# 添加字典中
mdict = {}
for i in mset:
    mcout = mlist.count(i)
    mdict[i] = mcout

# print(mdict)

# 根据出现次数降序
sort_dict = sorted(mdict.items(), key=lambda x:x[1], reverse=True)
print(sort_dict)
发布了221 篇原创文章 · 获赞 113 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42363032/article/details/104605259