博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
组合数据类型练习,英文词频统计实例上
阅读量:5345 次
发布时间:2019-06-15

本文共 3319 字,大约阅读时间需要 11 分钟。

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('\n',' ')
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

你可能感兴趣的文章
25复杂链表的复制
查看>>
2 Orchard汉化资源包的使用
查看>>
python3 property
查看>>
自定义控件注意点
查看>>
SSRS 报表 如何匿名查看
查看>>
JVM内存管理机制
查看>>
centos 安装Mysql
查看>>
简单通用Ajax函数
查看>>
【Android】ListView监听上下滑动(判断是否显示返回顶部按钮
查看>>
HBASE的MAPREDUCE任务运行异常解决办法,无需CYGWIN,纯WINDOWS环境
查看>>
禅道在docker上部署与迁移
查看>>
关于继承、封装、多态、抽象和接口
查看>>
c27---typedef
查看>>
android WebViewClient和WebChromeClient
查看>>
div+css清除浮动代码
查看>>
017Python路--解释器
查看>>
idea2019中utf-8乱码问题
查看>>
docker应用-3(搭建hadoop以及hbase集群)
查看>>
Luogu4191 [CTSC2010]性能优化【多项式,循环卷积】
查看>>
Oracle 相关查询
查看>>