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

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

dict={"1":"60","2":"56","3":"82","4":"74","5":"59"}

print("查询2号的成绩")
print(dict.get('2'))
print("删除3号的记录")
dict.pop('3')
print(dict.items())
print("将1的成绩改为100")
dict['1']=100
print(dict.items())
print("添加18号学生")
dict["18"]="100"
print(dict.items())
print("遍历:")
for i in dict:
print(i)

 

 

 

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

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

 

A=('0123456')
B=('abcdefg')
C=list('abcdefg')
D=set(C)
E=tuple('abcdefg')
F=dict(zip(A,B))
print(F)
print('打印列表')
for y in C:
print(y)
print('打印字典')
for x in F:
print(x,F[x])
print('打印元祖')
for y in E:
print(y)
print('打印集合')
for y in D:
print(y)

区别:

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

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

元组用“()”表示。元组是只读的,不能修改。

字典用“{}”表示。字典存储键值对(key-value)数据。

集合没有特殊的表示方法,而是通过一个set函数转换成集合。

 

3,英文词频统计实例

    待分析字符串

      分解提取单词

      大小写 txt.lower()

      分隔符'.,:;?!-_’单词列表

      单词计数字典

 

A='''Twinkle, twinkle, little star, How I wonder what you are.
Up above the world so high, Like a diamond in the sky.
Twinkle, twinkle, little star, How I wonder what you are!
When the blazing sun is gone,
When he nothing shines upon,
Then you show your little light,
Twinkle, twinkle, all the night.
Twinkle, twinkle, little star,
How I wonder what you are!
Then the traveler in the dark Thanks you for your tiny spark;
He could not see which way to go, If you did not twinkle so.
Twinkle, twinkle, little star, How I wonder what you are!
Twinkle Twinkle Little Star'''
print('待分析字符串',A)

for i in ',.?!\n':
A=A.replace(i,' ')
A=A.lower()
A=A.split(" ")
print('单词计数词典')

words=set(A)

dt={}
dt['star']=A.count('star')
for j in words:
dt[j]=A.count(j)
for j in dt:
print("{0:<11}{1}".format(j,dt[j]))

 

posted @ 2017-09-22 17:29  013洪辉  阅读(107)  评论(0编辑  收藏  举报