组合数据类型练习,英文词频统计实例上

1、

d={'001':89,"002":68,"003":87,"004":76,"005":71,"006":92}
print(d["001"])
print("007" in d)
print(d.keys())
print(d.values())
print(d.items())
print(d.pop("001"))
print(d.get("005","查无此人"))
del(d["003"])
print(d)

 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、英文词频统计实例

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符'.,:;?!-_’
    3. 单词列表
  3. 单词计数字典
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 ",.?!
":
   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]))

原文地址:https://www.cnblogs.com/Naiky/p/7573257.html