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

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

dict={'01':90,'02':85,'03':85,'04':75}
print('学生学号成绩:',dict)
dict['05']=80
print('增加学号为05的学生的成绩为80,即',dict)
dict.pop('03')
print('删除学号为03的学生成绩',dict)

print('查找学号为03的学生成绩',dict.get('03'))
print('查找06的学生成绩',dict.get('06','没有该学生'))
dict['04']=70
print('修改学号为04的学生成绩为70',dict)

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

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

 

nums=('123456789')
words=('abcdefg')
ls=list('abcdefg')
s=set(ls)
tu=tuple('abcdefg')
di=dict(zip(nums,words))
print(di)
print('打印列表')
for i in ls:
    print(i)
print('打印字典')
for j in di:
     print(j,di[j])
print('打印元组')
for i in tu:
    print(i)
print('打印集合')
for i in s:
    print(i)

 

       联系与区别 : 列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素,区别于元组,可动态增加,删除,更新;元组和列表在结构上没有什么区别,但元组是只读的,不能修改。元组用“()”表示,元组一旦定义其长度和内容都是固定的,即不能对元组进行更新、增加、删除操作;集合内的元素没有重复的元素,通过一个set函数转换成集合,是一个无序不重复元素集;字典存储键值对数据,字典最外面用大括号,每一组用冒号连起来,然后各组用逗号隔开,字典最大的价值是查询,通过键,查找值,字典和集合都是无序的。

 

3.英文词频统计实例

    (1)待分析字符串

    (2)分解提取单词

           a. 大小写 txt.lower()

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

           c.单词列表

    (3)单词计数字典

str='''Waking up I see that everything is okay
The first time in my life and now it's so great
Slowing down I look around and I am so amazed
I think about the little things that make life great
I wouldn't change a thing about it
This is the best feeling
This innocence is brilliant, I hope that it will stay
This moment is perfect, please don't go away
I need you now
And I'll hold on to it, don't you let it pass you by
I found a place so safe, not a single tear
The first time in my life and now it's so clear
Feel calm, I belong, I'm so happy here
It's so strong and now I let myself be sincere
I wouldn't change a thing about it
This is the best feeling
This innocence is brilliant, I hope that it will stay
This moment is perfect, please don't go away
I need you now
And I'll hold on to it, don't you let it pass you by
It's the state of bliss you think you're dreaming
It's the happiness inside that you're feeling
It's so beautiful, it makes you wanna cry'''
print('待分析字符串:\n',str)

for i in ',.?!\n':
    str=str.replace(i,' ')
str=str.lower()
str=str.split(" ")
words=set(str)
dt={}
dt['i']=str.count('i')
dt['so']=str.count('so')
dt['innocence']=str.count('innocence')
dt['you']=str.count('you')
print('单词计数字典:')
for j in words:
    dt[j]=str.count(j)
for j in dt:
    print("{0:<11}{1}".format(j,dt[j]))

 

posted @ 2017-09-26 11:43  105杨捷丽  阅读(162)  评论(0编辑  收藏  举报