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

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


d={'01':'90','02':'95','03':'89','04':'92','05':'88'}
print(d)


d['06']=97
print('增加一个学号为06的学生信息:',d)


print('查找出学号05的学生成绩:',d['05'])


d.pop('04')
print('删除学号为04的学生信息:',d)


d['01']=93
print('修改学号为01的学生成绩为93:',d)


print('遍历:')
for i in d:
    print(i,d[i])

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

l=['16','26','36']
print('列表遍历:')
for i in l:
print(i)


t=('46','56','66')
print('元组遍历:')
for i in t:
print(i)


d={'1':'76','2':'86','3':'96'}
print('字典遍历:')
for i in d:
print(i,d[i])


s=set([12,22,32])
print('集合遍历:')
for i in s:
print(i)


总结列表,元组,字典,集合的联系与区别。
(1)列表是任意对象的序列。列表用方括号表示。
(2)将一组值打包到一个对象中,称为元组。元组用圆括号表示。元组和列表的大部分操作相同。但是,列表是不固定的,可以随时插入,删除;而元组一旦确认就不能够再更改。
所以,系统为了列表的灵活性,就需要牺牲掉一些内存;而元组就更为紧凑。
(3)与列表和元组不同,集合是无序的,也不能通过索引进行访问。此外,集合中的元素不能重复。
(4)字典就是一个关联数组或散列表,其中包含通过关键字索引的对象。用大括号表示。与集合相比,通过关键字索引,所以比集合访问方便。字典是Python解释器中最完善的数据类型。

3.英文词频统计实例

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

 

s = '''I'm a big big girl !In a big big world ! It's not a big big thing if U leave me.But I do do feel.That I too too will miss U much.But I do feel I will miss U much ! Miss U much ! I can see the first leaf falling.It's all yellow & nice.It's so very cold outside.Like the way I'm feeling inside.Outside it's now raining.And tears are falling from my eyes.I have Ur arms around me ooooh like fire.But when I open my eyes.U're gone ! I'm a big big girl ! In a big big world ! It's not a big big thing if U leave me.But I do do feel.That I too too will miss U much.But I do feel I will miss U much ! Miss U much ! '''
s=s.lower()
print('全部转为小写:')
print(s)for i in ',.?!:':   
 s=s.replace(i,' ')
print('分隔符替换为空格:')
print(s)
words=s.split(' ')
print('分隔结果为:')
print(words)d={}
for i in words:   
 d[i]=words.count(i)
d=list(d.items())
print('单词计数字典:')
print(d)

posted on 2017-09-26 20:15  020吴惠琳  阅读(194)  评论(0)    收藏  举报