组合数据类型练习、英语词频统计
- 字典实例:建立学生学号成绩字典,做增删改查遍历操作。
di={} di["001"]=76 di["002"]=87 di["003"]=79 di["004"]=98 di["005"]=72 print(di) di["006"]=95 print(di) del(di['005']) print(di) for i in di: print(i,di[i])
- 列表,元组,字典,集合的遍历。
总结列表,元组,字典,集合的联系与区别。w=list('154896') e=tuple('564255') t=dict(zip(w,e)) u=set([5,3,8,8,6,1]) for i in w: print(i) for i in e: print(i) for i in t: print(i,t[i]) for i in u: print(i)
列表 元组 字典 集合 可否读写 可读写 只读 可读写 可读写 可否重复 是 是 是 否 存储方式 值 值 键值对(键不可重复) 键(不可重复) 是否有序 是 是 否 否 添加 append d[key]=value add - 英文词频统计实例
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符'.,:;?!-_’
- 单词列表
- 单词计数字典
lp='''You hid your skeletons, when I had shown you mine
you woke the devil that I thought you left behind
I saw the evidence the crimson soaking through
ten thousand promises, ten thousand ways to lose...And you held It all
but you were careless to let it fall
you held it all
and I was by your side
PowerlessI watched you fall apart
and chased you to the end
I'm left an empty mess, that words can not defend
you'll never know what I became
because of you
ten thousand promises, ten thousand ways to lose...And you held It all
but you were careless to let it fall'''
less=lp.lower()
print(less)
for i in ",...'":
lp=less.replace(i," ")
print(lp)
power=lp.split()
print(power)dic={}
for i in power:
dic[i]= power.count(i)
lp=list(dic.items())print('单词计数字典:',lp)
)