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


    1.
    name=['陈楠芸','陈文琪','刘书签','杨必须'] scores=[7,6,6,5] d={'陈楠芸':7,'陈文琪':6,'刘书签':6,'杨必须':5} print(d) #增加 d['薛宝宝']=5 print(d) #删除 d.pop('陈文琪') print(d) #修改 d['刘书签']=5 print(d) #查询 print(d.items())

        

    name=['陈楠芸','陈文琪','刘书签','杨必须']
    scores=[7,6,6,5]
    d={'陈楠芸':7,'陈文琪':6,'刘书签':6,'杨必须':5}
    print(d)
    #增加
    d['薛宝宝']=5
    print(d)
    #删除
    d.pop('陈文琪')
    print(d)
    #修改
    d['刘书签']=5
    print(d)
    #查询
    print(d.items())

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

       列表查找和插入的时间随元素的增加而增加,占用空间小,浪费内存很少;列表可以有N个元素,元素的类型是任意的,与列表本身无关。字典的查找和插入速度极快,不会随key的增加而变慢,需要占用大量内存,内存浪费多。字典使用键—值存储名具有极快的查找速度。字典的key必须是不可变对象。Python的集合(set)和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.由于集合是无序的,所以,sets 不支持 索引, 分片, 或其它类序列(sequence-like)的操作。

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

    #列表
    l = list('23132111123132')
    #元组
    t = tuple('1233211232123')
    #字典
    d = dict(zip([1,2,3,4],[4,3,2,1]))
    #集合
    s = set(l)
    for i in l:
    print(i)
    for i in t:
    print(i)
    for i in s:
    print(i)
    for i in d:
    print(i)

    英文词频统计实例

    1. 待分析字符串
    2. 分解提取单词
      1. 大小写 txt.lower()
      2. 分隔符'.,:;?!-_’
      3. 单词列表
    3. 单词计数字典
    4. 结果

      ===================== RESTART: C:/Users/陈楠芸/Desktop/e.py =====================
      dict_items([('"jack', 1), ('holidayit', 1), ('monsters.', 1), ('bags', 1), ('faces', 2), ('or', 5), ('carry', 1), ('money', 1), ('this', 1), ('americans', 1), ('cut', 1), ('costumes', 1), ('them', 1), ('also', 2), ('orange', 1), ('holiday', 2), ('all', 1), ('look', 1), ('were', 1), ('most', 1), ('come', 1), ('in', 2), ('put', 3), ('they', 4), ('not', 1), ('"holy', 1), ('31the', 1), ('are', 2), ('an', 1), ('candy', 1), ('then', 1), ('burning', 1), ('only', 1), ('october', 1), ('ghost', 1), ('howeverit', 1), ('mainly.u3000u3000every', 1), ('will', 1), ('ready', 1), ('autumn', 1), ('if', 1), ('satisfaction', 1), ('evening', 2), ('them.this', 1), ('pumpkin!', 1), ('treat!', 1), ('as', 3), ('comes', 1), ('candle', 1), ('imaginations', 1), ('church', 1), ('vegetables', 1), ('because', 1), ('themselves', 1), ('day.', 1), ('day', 1), ('inside.it', 1), ('really', 1), ('these', 1), ('housethey', 1), ('house', 1), ('personages', 1), ('children', 3), ('frightening', 1), ('celebrate', 1), ('large', 1), ('lantern".u3000u3000the', 1), ('before', 1), ('"', 1), ('saints‘', 1), ('lead', 1), ('pick', 1), ('house.every', 1), ('called', 1), ('love', 1), ('autumnwhen', 1), ('pumpkins', 1), ('a', 5), ('there', 1), ('it', 1), ('paint', 1), ('being', 1), ('year.it', 1), ('eatchildren', 1), ('means', 2), ('bags.u3000u3000not', 1), ('every', 3), ('can', 1), ('halloween', 3), ('halloween.some', 1), ('parties', 1), ('person', 1), ('lights', 1), ('to', 4), ('jack-o‘-lanternswhich', 1), ('childrenbut', 1), ('eat!"the', 1), ('for', 1), ('the', 5), ('new', 1), ('on', 2), ('young.', 1), ('masks', 1), ('is', 3), ('out', 1), ('treat-money', 1), ('grown-ups', 2), ('say', 1), ('strange', 1), ('boxes', 1), ('from', 1), ('"trick', 1), ('their', 3), ('of', 3), ('disguise', 1), ('time', 1), ('like', 1), ('and', 4), ('looking', 1), ('pumpkins.then', 1), ('looks', 1), ('that', 1), ('bring', 1)])
      >>>

    a='''halloween is an autumn holiday that americans celebrate every year.
        it means "holy evening," and it comes every october 31, the evening before all saints‘ day. however, it is not really a church holiday, it is a holiday for children mainly.
      every autumn, when the vegetables are ready to eat, children pick large orange pumpkins.
    then they cut faces in the pumpkins and put a burning candle inside.
    it looks as if there were a person looking out of the pumpkin! these lights are called jack-o‘-lanterns, which means "jack of the lantern".
      the children also put on strange masks and frightening costumes every halloween.
    some children paint their faces to look like monsters. then they carry boxes or bags from house to house.
    every time they come to a new house, they say,"trick or treat! money or eat!"
    the grown-ups put treat-money or candy in their bags.
      not only children, but most grown-ups also love halloween and halloween parties because on this day,they can disguise themselves as personages or ghost as their imaginations will lead them.
    this bring them the satisfaction of being young.'''
     # #将,?.!变成空格
    a= a.replace(',',' ')
    a= a.replace('
    ','')
    a= a.replace('  ','')
    #将所有大写转换为小写
    a = a.lower()
    #把歌词切片
    words = a.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)


  • 相关阅读:
    『空』
    退役前的做题记录 Ⅰ
    BZOJ3600 没有人的算术(替罪羊树,线段树)
    洛谷P5324 [BJOI2019]删数(线段树)
    洛谷P4696 [CEOI2011]Matching(KMP)
    Leetcode 638 大礼包 DP
    Leetcode 86 分割链表
    Leetcode 71 简化路径
    Leetcode 17.15 最长单词 剪枝与记忆化
    Leetcode 17.22单词转换 dfs+回溯+剪枝
  • 原文地址:https://www.cnblogs.com/YyYyYy11/p/7573319.html
Copyright © 2020-2023  润新知