码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-26 19:41:19      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:for   创建   elf   sel   logs   mat   有序   字典   列表   

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

dict={02:89,"03":90,"04":100,"05":75}
print("查询02学号的成绩")
print(dict.get(02))
print("删除03号的记录")
dict.pop(03)
print(dict.items())
print("01学号的成绩改为99")
dict[01]=99
print(dict.items())
print("添加06号学生")
dict["06"]=80
print("查找:")
print(dict.get(05,不存在))
print(dict.get(10,不存在))
print(dict.items())
print("遍历")
for i in dict:
 print(i)

技术分享

 

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

lis=["01","02","03","04"]
tu=("01","02","03","04","05")
dic={"01":"91","02":"92","03":"93","04":"94"}
s=set(lis)

for i in lis:
    print(i)

for i in tu:
    print(i)

for i in dic:
   print({0:<}:{1:>}.format(i,dic[i]))

for i in s:
    print(i)

技术分享

答:列表是有序的元素集合,元组与列表很相像,但是最大的区别就是元组只能被读取而不能被修改。字典是以键值对作为单位来进行存放的集合。集合(set)则是一组key的无序集合,但是它只包含key,key值不能够重复,并且需要先创建一个List作为输入集合才能继续创建set。

 

3.英文词频统计实例

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符‘.,:;?!-_’
    3. 单词列表
  3. 单词计数字典
ordinary=‘‘‘This is just an ordinary day,
Wipe the insecurities away. 
I can see that the darkness will erode, 
Looking out the corner of my eye,.
I can see that the sunshine will explode,
Far across the desert in the sky. 
Beautiful girl won‘t you be my inspiration, 
Beautiful girl don‘t you throw your love around.
What in the world, what in the world could ever come between us,
beautiful girl, beautiful girl. 
I‘ll never let you down, won‘t let you down, 
This is the beginning of your day. 
Life is more intricate than it seems, 
Always be yourself along the way. 
Living through the spirit of your dreams.‘‘‘

ordinary=ordinary.lower()

for i in ,.\"?:
    ordinary=ordinary.replace(i, )

words=ordinary.split( )
print(words)

dict={}
for i in words:
    dict[i]=words.count(i)
print(dict)

 

 





 

 

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

标签:for   创建   elf   sel   logs   mat   有序   字典   列表   

原文地址:http://www.cnblogs.com/xy223/p/7595967.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!