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

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

classmates={'01':95,'02':88,'03':65,'04':71,'05':79}
#
classmates['06']=97
#
classmates.pop('01')
#
classmates['03']=98
#
classmates.get('05')
print(classmates)

运行结果是:

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

(1)列表:

letter=['a','b','c','d','e']
for i in range(len(letter)):
    print(letter[i])

运行结果是:

(2)元组:

letter=('a','b','c')
for i in range(len(letter)):
    print(letter[i])

运行结果是:

 (3)字典:

score={'Amy':90,'Bob':85,'Cindy':88,'Dave':76,'Fiona':78}
for key in score:
    print(score[key])

运行结果是:

(4)集合:

weekdays=set(['MON','TUE','WED','THU','FRI','SAT','SUN'])
for d in weekdays:
    print (d)

运行结果是:

 


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

 列表和元组是有序的,字典和集合都是无序的。

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

元组用“()”表示。元组是只读的,不能修改。元组一旦定义其长度和内容都是固定的。

字典用“{}”表示。字典存储键值对(key-value)数据。每一组用冒号连起来,然后各组用逗号隔开。

集合没有特殊的表示方法,而是通过一个set函数转换成集合。集合内的元素没有重复的元素,主要是消除重复元素。

3.英文词频统计实例

      1. 待分析字符串
      2. 分解提取单词
        1. 大小写 txt.lower()
        2. 分隔符'.,:;?!-_’
        3. 单词列表
      3. 单词计数字典
song='''I don't like your little games
Don't like your tilted stage
The role you made me play of the fool
No I don't like you
I don't like your perfect crime
How you laugh when you lie
You said the gun was mine
Isn't cool
No I don't like you ooh
But I got smarter I got harder in the nick of time
Honey I rose up from the dead I do it all the time
I got a list of names and yours is in red underlined
I check it once then I check it twice ooh
Ooh look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me
Ooh look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me do
I don't like your kingdom keys
They once belonged to me
You asked me for a place to sleep
Locked me out and threw a feast what
The world moves on another day another drama drama
But not for me not for me all I think about is karma
And then the world moves on but one thing's for sure
Maybe I got mine but you'll all get yours
But I got smarter I got harder in the nick of time
Honey I rose up from the dead I do it all the time
I've got a list of names and yours is in red underlined
I check it once then I check it twice ooh
Ooh look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me
Ooh look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me do
I don't trust nobody and nobody trusts me
I'll be the actress starring in your bad dreams
I don't trust nobody and nobody trusts me
I'll be the actress starring in your bad dreams
I don't trust nobody and nobody trusts me
I'll be the actress starring in your bad dreams
I don't trust nobody and nobody trusts me
I'll be the actress starring in your bad dreams
Look what you made me do
Look what you made me do
Look what you made me do
I'm sorry the old Taylor can't come to the phone right now
Why
Oh 'cause she's dead ohh
Ooh look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me
Ooh look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me do
Ooh look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me
Ooh look what you made me do
Look what you made me do
Look what you just made me do
Look what you just made me do'''
song=song.lower()
song=song.replace('
',' ')
word=song.split(' ')
dic={}
keys=set(word)
for i in keys:
    dic[i]=word.count(i)
print(dic)

运行结果是:

原文地址:https://www.cnblogs.com/iamzhuangyuan/p/7573334.html