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

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

时间:2017-09-26 13:05:52      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:单词   区别   成绩   any   hat   size   some   bsp   sort   

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

dic={055:80,056:81,057:82,058:83,059:84,060:85,061:86,062:87,063:88,064:89}
dic[065]=90   //增
dic.pop(055)    //删
dic[056]=91     //改
dic.get(070,不知道)   //070 in dic    //for i in dic:
    print(i,dic[i])  //遍历

 

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

l=list(123456123)
for i in l:
    print(i)     //列表遍历


t=tuple(123456123)
for i in t:
    print(i)     //元祖遍历


d={055:80,056:81,057:82}
for i in d:
    print(i,d[i])     //字典遍历


s=set(123456123)
for i in s:
    print(i)    //集合遍历

 


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

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

3.英文词频统计实例

  1. 待分析字符串分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符‘.,:;?!-_’
    3. 单词列表
  2. 单词计数字典
    g=‘‘‘Say something, I‘m giving up on you.
    I‘ll be the one, if you want me to.
    Anywhere, I would‘ve followed you.
    Say something, I‘m giving up on you.
    And I am feeling so small.
    It was over my head
    I know nothing at all.
    And I will stumble and fall.
    I‘m still learning to love
    Just starting to crawl.
    Say something, I‘m giving up on you.
    I‘m sorry that I couldn‘t get to you.
    Anywhere, I would‘ve followed you.
    Say something, I‘m giving up on you.
    And I will swallow my pride.
    You‘re the one that I love
    And I‘m saying goodbye.
    Say something, I‘m giving up on you.
    And I‘m sorry that I couldn‘t get to you.
    And anywhere, I would have followed you.
    Oh-oh-oh-oh say something, I‘m giving up on you.
    Say something, I‘m giving up on you.
    Say something...‘‘‘
    g=g.lower()     // 小写
    for i in ,.:
        g=g.replace(i, )    //替换
    words=g.split( )       //分隔
    di = {}
    words.sort()
    disc = set(words)
    for i in disc:
        di[i] = 0
    for i in words:
        di[i] = di[i]+1
    items = di.items()
    print(items)

     

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

标签:单词   区别   成绩   any   hat   size   some   bsp   sort   

原文地址:http://www.cnblogs.com/misakihong/p/7596010.html

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