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

1、字典实例:建立学生学号成绩字典,做增删改查遍历操作。

a={'01':'95','02':'80','03':'98','04':'88','05':'92','06':'85'}
print(a.keys())
print(a.values())
a['07']=89
a['06']=86
a.pop('05')
print(a.get('04'))
print(a)

 

2、列表,元组,字典,集合的遍历。

     总结列表,元组,字典,集合的联系与区别。

l = list('123212131231231')
t = tuple('abcdefghijklmn')
s = set(l)
d = dict(zip([1,2,3,4],[4,3,2,1]))
print(l)
for i in l:
print(i)
for j in t:
print(j)
for k in s:
print(k)
for m in d:
print(m)

列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。

元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用()表示。元组一旦定义其长度和内容都是固定的。

 集合是一个无序不重复元素集

字典最外面用大括号,每一组用冒号连起来,然后各组用逗号隔开,字典最大的价值是查询,通过键,查找值。

3、英文词频统计实例

A、待分析字符串

B、分解提取单词

a、大小写 txt.lower()

b、分隔符'.,:;?!-_’

c、单词列表

C、单词计数字典

t='''Wake up in the morning feeling like P Diddy
Put my glasses on, I'm out the door
I'm gonna hit this city (Let's go)
Before I leave,
Brush my teeth with a bottle of Jack
Cause when I leave for the night,
I ain't coming back
I'm talking - pedicure on our toes, toes
Trying on all our clothes, clothes
Boys blowing up our phones, phones
Drop-toping, playing our favorite CDs
Pulling up to the parties
Trying to get a little bit tipsy
Don't stop, make it pop
DJ, blow my speakers up
Tonight, Imma fight
Till we see the sunlight
Tick tock, on the clock
But the party don't stop
Woah-oh oh oh
Woah-oh oh oh
Don't stop, make it pop
DJ, blow my speakers up
Tonight, Imma fight
Till we see the sunlight
Tick tock, on the clock
But the party don't stop
Woah-oh oh oh
Woah-oh oh oh
Ain't got a care in world,
But got plenty of beer
Ain't got no money in my pocket,
But I'm already here
And now, the dudes are lining up
Cause they hear we got swagger
But we kick 'em to the curb
unless they look like Mick Jagger
I'm talking about
everybody getting crunk, crunk
Boys trying to touch my junk, junk
Gonna smack him if he getting too drunk, drunk
Now, now - we goin' til they kick us out, out
Or the police shut us down, down
Police shut us down, down
Po-po shut us -
Don't stop, make it pop
DJ, blow my speakers up
Tonight, Imma fight
Till we see the sunlight
Tick tock, on the clock
But the party don't stop
Woah-oh oh oh
Woah-oh oh oh
Don't stop, make it pop
DJ, blow my speakers up
Tonight, Imma fight
Till we see the sunlight
Tick tock, on the clock
But the party don't stop
Woah-oh oh oh
Woah-oh oh oh
DJ, you build me up
You break me down
My heart, it pounds yeah, you got me
With my hands up
You got me now
You gotta that sound yeah, you got me
DJ, you build me up
You break me down
My heart, it pounds yeah, you got me
With my hands up
Put your hands up
Put your hands up
No, the party don't stop until I walk in
Don't stop, make it pop
DJ, blow my speakers up
Tonight, Imma fight
Till we see the sunlight
Tick tock, on the clock
But the party don't stop
Woah-oh oh oh
Woah-oh oh oh
Don't stop, make it pop
DJ, blow my speakers up
Tonight, Imma fight
Till we see the sunlight
Tick tock, on the clock
But the party don't stop
Woah-oh oh oh
Woah-oh oh oh
'''
t=t.lower()
for i in ',.?!':
t=t.replace(i,' ')
t=t.replace(' ',' ')
words=t.split(' ')

s=set(words)
dic={}

lis=[]
value=[]
for i in s:
if(i==' '):
continue
if(i==''):
continue
dic[i]=words.count(i)
lis.append(words.count(i))
vgalue=dic.values()

lis=list(dic.items())
lis.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
print(lis[i])

原文地址:https://www.cnblogs.com/GAODASHANG/p/7573312.html