Python_Combined data type_practice190603

本文深入探讨Python的组合数据类型,包括列表、元组、集合和字典的使用技巧和实践案例,帮助读者掌握数据操作的核心技能。

思路:暂存当前最大词(为比较字符串大小)及其频率(为比较频率)
先比较频率,仅当频率相同时比较字符串大小

#coding:utf-8
#printice 3 data cleaning  and frequency statistics  
'''
输出长度大于2且最多的单词。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬
如果存在多个单词出现频率一致,
请输出按照Unicode排序后最大的单词。
'''
import jieba
story=open("C:/Users/UMR/Desktop/沉默.txt","rt")
txt=story.read()
story.close()
words=jieba.lcut(txt)#segment the words
count=dict()
for word in words:
    if len(word)<2:
        continue
    else:
        count[word]=count.get(word,0)+1
#请输出按照Unicode排序后最大的单词。
#思路:暂存当前最大词(为比较字符串大小)及其频率(为比较频率)
#先比较频率,仅当频率相同时比较字符串大小

nowmax=0 #当前最大频率单词的频率(int)
max='' #当前最大频率的单词(string)
for wd in count:
    #频率大的情况
    if count[wd]>nowmax:
        nowmax=count[wd]
        max=wd
    #频率相等的情况-->仅当 新词 大于 旧词时才替换
    if count[wd]==nowmax and wd>max:
        max=wd
print(max)




#practice 2 reverse the dictionary
try:
    inp=eval(input())
    print(type(inp))
    assert type(inp)==dict
    out=dict()
    for i  in inp:
        print(i,inp[i])
        out[inp[i]]=i
    print(out)
        
         
except Exception:
    print("输入错误")
    
#practice 1 data deduplication
s = '''双儿 洪七公 赵敏 赵敏 逍遥子 鳌拜 殷天正 金轮法王 乔峰 杨过 洪七公 郭靖 
       杨逍 忽必烈 忽必烈 张三丰 乔峰 乔峰 
       阿紫 乔峰 金轮法王 袁冠南 张无忌 郭襄 黄蓉 李莫愁 赵敏 赵敏 郭芙 张三丰 
      七公 郭芙 郭襄'''
names=s.split()
namedic={}
for name in names:
    namedic[name]=namedic.get(name,0)+1
out=list(namedic.items())
print(out)
out.sort(key=lambda x:x[1], reverse=True)
print(out)
print(len(out))

#use set to deduplication
namelen=set(s.split())
print(namelen)

print(len(namelen))
#printice 3 data cleaning  and frequency statistics
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\UMR\AppData\Local\Temp\jieba.cache
Loading model cost 2.669 seconds.
Prefix dict has been built succesfully.
史达琳
#practice 2 reverse the dictionary
sfsasf
输入错误
------------------
{"a": 1, "b": 2}
<class 'dict'>
a 1
b 2
{1: 'a', 2: 'b'}
#practice 1 data deduplication
[('双儿', 1), ('洪七公', 2), ('赵敏', 4), ('逍遥子', 1), ('鳌拜', 1), ('殷天正', 1), ('金轮法王', 2), ('乔峰', 4), ('杨过', 1), ('郭靖', 1), ('杨逍', 1), ('忽必烈', 2), ('张三丰', 2), ('阿紫', 1), ('袁冠南', 1), ('张无忌', 1), ('郭襄', 2), ('黄蓉', 1), ('李莫愁', 1), ('郭芙', 2), ('七公', 1)]
[('赵敏', 4), ('乔峰', 4), ('洪七公', 2), ('金轮法王', 2), ('忽必烈', 2), ('张三丰', 2), ('郭襄', 2), ('郭芙', 2), ('双儿', 1), ('逍遥子', 1), ('鳌拜', 1), ('殷天正', 1), ('杨过', 1), ('郭靖', 1), ('杨逍', 1), ('阿紫', 1), ('袁冠南', 1), ('张无忌', 1), ('黄蓉', 1), ('李莫愁', 1), ('七公', 1)]
21
{'乔峰', '杨逍', '阿紫', '鳌拜', '洪七公', '郭襄', '金轮法王', '张无忌', '郭芙', '殷天正', '逍遥子', '袁冠南', '七公', '赵敏', '双儿', '郭靖', '忽必烈', '杨过', '张三丰', '李莫愁', '黄蓉'}
21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值