组合数据类型练习,英文词频统计实例上
1、字典实例:建立学生学号成绩字典,做增删改查遍历操作。
dict
=
{
"01"
:
"90"
,
"02"
:
"91"
,
"03"
:
"92"
,
"04"
:
"94"
,
"05"
:
"95"
,
"06"
:
"96"
,
"07"
:
"97"
,
"08"
:
"98"
}
print
(
"查询02学号的成绩"
)
print
(
dict
.get(
'02'
))
print
(
"删除07号的记录"
)
dict
.pop(
'07'
)
print
(
dict
.items())
print
(
"将01的成绩改为100"
)
dict
[
'01'
]
=
100
print
(
dict
.items())
print
(
"添加18号学生"
)
dict
[
"18"
]
=
"100"
print
(
dict
.items())
print
(
"遍历:"
)
for
i
in
dict
:
print
(i)
2、列表,元组,字典,集合的遍历,总结列表,元组,字典,集合的联系与区别。
代码如下:
ss=set("1234543") sa=set("3456") print(ss&sa) print(ss|sa) print(ss-sa) for i in ss: print(i,end='') print() lis=list("1234567") tu=tuple("dhrcnji") dt=dict(zip(lis,tu)) for i in lis: print(i,end='') print() for i in tu: print(i,end='') print() for i in dt: print(i,dt[i])
3、英文词频统计实例
A、待分析字符串
B、分解提取单词
a.大小写 txt.lower()
b.分隔符'.,:;?!-_’
c.单词列表
C、单词计数字典
str='''We don't talk anymore We don't talk anymore We don't talk anymore Like we used to do We don't laugh anymore What was all of it for? We don't talk anymore Like we used to do I just heard you found the one you've been lookin' The one you been looking for I wish i would've konwn that wasn't me Cause even after all this time i still wonder Why i can't move on? Just the way you dance so easliy Don't wanna know The kinda dress you're wearin' tonight If he's holdin' onto you so tight The way i did before I overdosed Should've known your love was game Now I can't get'cha out of my brain Ooh it's such a shame We don't talk anymore We don't talk anymore We don't talk anymore Like we used to do We don't laugh anymore What was all of it for? We don't talk anymore Like we used to do I just hope you'r lyin' next to somebody Know it's hard to love ya like me Must be a good reason that you're gone Every now and then I think you might want me to come show up your door But I'm just too afraid that i'll be worng Don't wanna know If you'ra lookin' into her eyes If she's holdin' onto you so tight The way i did before I overdosed Should've know your love was a game Now I can't get'cha out of my brain Ooh it's such a shame We don't talk anymore We don't talk anymore We don't talk anymore Like we used to do We don't laugh anymore What was all of it for? We don't talk anymore Like we used to do Like we used to do Don't wanna know The kinda dress you're wearin' tonight If he's givin' it to you just right The way i did before I overdosed Should've know your love was a game Now I can't get'cha out of my brain Ooh it's such a shame We don't talk anymore We don't talk anymore We don't talk anymore Like we used to do We don't laugh anymore What was all of it for? We don't talk anymore Like we used to do We don't talk anymore The way did before We don't talk anymore Ooh Woo Ooh it's such a shame We don't talk anymore ''' for i in ",.?!\n": str=str.replace(i," ") str=str.lower() str=str.split(" ") words=set(str) dt={} dt["we"]=str.count("we") dt["talk"]=str.count("talk") for i in words: dt[i]=str.count(i) for i in dt: print("{0:<10}{1}".format(i,dt[i]))