组合数据类型练习、英语词频统计

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

di={'9':'19','29':'39','49':'59','69':'79','89':'99'}
print(di) #创建字典

di['88']=89
print('增加一个学号为88的学生信息:',di) #增加

print('查找出学号29的学生成绩:',di['29']) #查找

del(di['9'])
print('删除学号为9的学生信息:',di) #删除

di['59']=77
print('修改学号为59的学生成绩为77:',di) #修改

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

 

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

a=['aa','bb','cc']
print('列表遍历:')
for i in a:
    print(i)  

b=('11','22','33')
print('元组遍历:')
for i in b:
    print(i)  

c={'a':'11','b':'22','c':'33'}
print('字典遍历:')
for i in c:
    print(i,c[i]) 

d=set([99,88,77,66,55])
print('集合遍历:')
for i in d:
    print(i) 

(1)列表是处理一组有序项目的数据结构,即在一个列表中存储一个序列的项目。列表中的项目。列表中的项目应该包括在方括号中。一旦创建列表,可以添加,删除,或者是搜索列表中的项目。列表是可变的数据类型,并且列表是可以嵌套的。

(2)元组是不能修改、不可变的。通过圆括号和逗号分隔对项目进行定义。元组通常用在使语句或用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变。元组可以嵌套。

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

(4)集合就是我们数学学的集合,没有什么特殊的定义。集合最好的应用是去重。集合没有特殊的表示方法,而是通过一个set函数转换成集合。

 

3. 英文词频统计实例

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符'.,:;?!-_’
    3. 单词列表
  3. 单词计数字典
lp='''I'm a little used to wandering outside the rain
  You could leave me tomorrow if it suits you just the same
  But I don't know enough, I need sun when it leaves the day
'Cause it's hard for me to lose in my life I've found
Only time will tell and I will figure out
  That we can baby, we can do the one night stand
  And it's hard for me to lose in my life I've found
  Outside your skin right near the fire
  That we can baby, we can change and feel alright '''
less=lp.lower()
print(less)
for i in ",...'":
    lp=less.replace(i," ")
print(lp)
power=lp.split()
print(power)

dic={}

for i in power:
    dic[i]= power.count(i)
lp=list(dic.items())

print('单词计数字典:',lp)

 

posted @ 2017-09-26 21:22  杜丽晖  阅读(183)  评论(0编辑  收藏  举报