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

这一生的挚爱 提交于 2020-02-21 22:06:18

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

d={}
name=['a','b','c','kris']
sco=[65,75,85,100]
ok=dict(zip(name,sco))
#增#
ok['mine']=95
#删
ok.pop('c')
#改
ok['a']="70"

 

 

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

  答:区别:

1.列表,元组,字典是有顺序的,而集合是没顺序的

2.列表是以方括号形式表示,元组是以圆括号表示,字典以花括号表示,集合则是以[()]的形式表示

3.列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素,区别于元组,可动态增加,删除,更新。

4.元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示。元组一旦定义其长度和内容都是固定的。一旦创建元组,则这个元组就不能被修改,即不能对元组进行更新、增加、删除操作。若想创建包含一个元素的元组,则必须在该元素后面加逗号“,”,否则创建的不是一个元组,而是一个字符串。

5.集合没有特殊的表示方法,而是通过一个set函数转换成集合。集合是一个无序不重复元素集,基本功能包括关系测试和消除重复元素.。

6.字典最大的价值是查询,通过键,查找值。

 

lst=['1','2','3','3','21']
print(lst)
for a in lst:
  print(a)

 

t=['1','2','3','3','1']
print(t)
for a in t:
  print(a)

 

d={}
d['a']='5'
d['b']='6'
d['c']='7'

for i in d:
    print(i,d[i])

 

s=set('123668833')

for i in s:
    print(i)

3英文词频统计实例

(1)待分析字符串

 

(2)分解提取单词

     大小写 txt.lower()

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

    单词列表

(3)单词计数字典

s='''KEVIN:
Boy,we made it now we on it.
Think about that time we grindin uh
Nothing's changed since that night we pieced up lullaby.
06 now add a 1 to that,
Ten years deep and we still on it.
Like we figured look who left on top the two of .
Could you imagine otherwise?
All that talk about that life we made it come alive.
No regrets or doubts!
Once we say we say we do,
Now the world behind our views,
Seeing the bigger picture man,
I knew the end it be the two.
KRIS:
Ye!
End will be the two,
You know we got the views,
I need some real reviews,
Tell me the truth what else I gotta do.
Don't wanna be stayin in the shadow rear mirror view.
Cause we got the sauce and we started from the bottom
Right about now!
I'll smile don't wanna see u cry!
Just sing me a lullaby.
I'll cry for u to see ur smile,
This will be my lullaby.
I'll smile don't wanna see u cry!
Just sing me a lullaby!
I'll cry for u to see ur smile.
This will be my lullaby.
KEVIN:
Didn't think a chimney would get me so deep run and chasin',
What seem to be fantasies ain't wasted,
Look at time fly by tick the clock counter-wise,
Now is just a moment caught picture perfect god,
Man patience u said was a virtue!
Maybe that's why the fire never dimmed and pursued,
Into something even God himself couldn't presume,
But thanks to you I stand here the dream continues.
KRIS:
I'll smile don't wanna see u cry,
Just sing me a lullaby.
I'll cry for u to see ur smile,
This will be my lullaby.
I'll smile don't wanna see u cry,
Just sing me a lullaby.
I'll cry for u to see ur smile,
This will be my lullaby.
I'll smile don't wanna see u cry,
Just sing me a lullaby.
I'll cry for u to see ur smile,
This will be my lullaby.
I'll smile don't wanna see u cry,Just sing me a lullaby.
I'll cry for u to see ur smile.
This will be my lullaby!'''

k=s.lower()
print(k)

for i in ',!?':
    a=s.replace(i,' ') 
    print(a)
    
b=s.split(' ')
print(b)

c= set(s)
dic={}
for i in c:
    dic[i]= b.count(i)
    
b=list(dic.items())
print('单数计数字典',b)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!