• 复合数据类型


    此次作业的要求来自于https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2696

    1.列表,元组,字典,集合分别如何增删改查及遍历。

    列表的增删改查及遍历操作:

    test = [ 'Michael' , 'Bob',33,'李四','Tracy','may']
    
    test[2]='aa'; #修改列表的值
    
    test.insert(1,'jack')#增加列表的值
    test.append('Cady');#在末尾增加列表的值
    test.pop(3)#根据元素下标修改列表的值
    
    x = test.index('may');
    print(x);#根据列表元素查找元素在列表中的下标号
    
    for i in list:
    print ("序号:%s 值:%s" % (list.index(i) + 1, i))#列表的遍历
    print(test)

    元组的增删改查及遍历操作:
    tu = (1, 2, 3, 'alex', [2, 3, 4, 'taibai'], 'egon')
    print(tu[2])  # 查找元祖下标3的元素
    
    print(tu[0:4])  # 查找元祖下标0-4的元素
    
    for i in tu:
        print(i)  #循环元祖里的元素,只循环一次。
    
    tu[4].append('ss')
    print(tu)  # 在元祖里面的列表里增加元素
    
    del tu; #删除整个元组


    字典的增删改查及遍历操作:
    test = {'Alice': 44, 'Beth': 55, 'Cecil': 76,'Cady':78,'Bob':79};
    
    # 字典里更改元素
    test['Beth'] = 65;
    print(test);
    
    # 字典增加信息
    test['Baby'] = 67;
    print(test);
    
    test = {'Alice': 95, 'Beth': 81, 'Cecil': 76,'Cady':87,'Bob':79,'Molly':86};
    
    # 删除字典的信息
    del test['Alice'];
    print(test);
    
    # 清空字典的信息
    test.clear();
    print(test);
    
    # 删除字典,使用下面语句后整个字典被删除
    del test

    
    

    集合的增删改查及遍历操作:

    test = {'Alice', 'Beth', 'Cecil', 'Cady', 'Bob', 'Molly'};
    
    # 添加某个元素到集合中
    test.add('Youth');
    print(test);
    
    # 添加元素到集合中
    test.update({123, 456});
    print(test);
    
    basket = {'orange', 'banana', 'pear', 'apple'};
    
    # 1.删除元素,如果元素不存在,则会发生错误
    test.remove('Alice');
    print(test);
    
    # 2.删除元素,如果元素不存在,不会发生错误
    test.discard('Beth');
    print(test);
    
    # 3.随机删除集合中的一个元素
    x = basket.pop();
    print("删除的元素是:", x);
    print(basket);

    2.总结列表,元组,字典,集合的联系与区别。参考以下几个方面:

    • 括号
    • 有序无序
    • 可变不可变
    • 重复不可重复
    • 存储与查找方式
      答:
    1. 列表用“[]”表示,元组则是用“()”表示,字典最外面用花括号{},每一组用冒号连起来,然后各组用逗号隔开。集合可以用set()函数或者方括号{}创建,元素之间用逗号”,”分隔。 
    2. 列表和元组都是有序的,而字典与集合为无序的。
    3. 列表中的元素可以是任意类型,也就是可变的序列,元组则属于不可变序列类型 ,字典也是与可变序列。
    4. 列表,元组,字典都可重复,集合则不可重复。

    列表和元组的存储与查找方式通过值来完成,而字典则是通过键值对(键不能重复)来完成,集合则是通过键(不能重复)来完成。
    总结:
    列表和元组有很多相似的地方,操作也差不多。不过列表是可变序列,元组为不可变序列。也就是说列表主要用于对象长度不可知的情况下,而元组用于对象长度已知的情况下,而且元组元素一旦创建变就不可修改。 字典主要应用于需要对元素进行标记的对象,这样在使用的时候便不必记住元素列表中或者元组中的位置,只需要利用键来进行访问对象中相应的值。集合中的元素不可重复的特点使它被拿来去重。


    3.词频统计

    1.下载一长篇小说,存成utf-8编码的文本文件 file

    2.通过文件读取字符串 str

    3.对文本进行预处理

    4.分解提取单词 list

    5.单词计数字典 set , dict

    6.按词频排序 list.sort(key=lambda),turple

    7.排除语法型词汇,代词、冠词、连词等无语义词

    自定义停用词表

    或用stops.txt

              8.输出TOP(20)
              输出的TOP(20)如下图所示:

              

             9.可视化:词云

    词云生成结果:



    线上工具生成词云:
    https://wordart.com/create

    exclude={  'i','me','my','myself', 'we','our', 'ours','ourselves', 'you', "you're", "you've","you'll", "you'd", 'your', 'yours', 'yourself','yourselves', 'he', 'him', 'his', 'himself', 'she',"she's",'her','hers', 'herself', 'it', "it's",
     'its', 'itself','they', 'them','their', 'theirs', 'themselves', 'what','which', 'who', 'whom', 'this', 'that', 'that', 'these','those','am','is','are','was','were','be','been',
    'being', 'have', 'has', 'had', 'having','do', 'does', 'did', 'doing','a','an','the','and','but','if','or','because','as','until','while','of','at','by','for','with','about',
     'against','between','into','through','during','before','after','above','below','to','from','up','down','in','out','on','off','over','under','again','further','then','once',
     'here','there','when','where','why','how','all','any','both','each','few','more','most','other','some','such','no','nor','not','only','own','same','so','than','too','very','s','t',
     'can','will','just','don',"don't",'should',"should've",'now','d','ll','m','o','re','ve','y','ain','aren',"aren't",'couldn',"couldn't",}#需要删除的无意义的词语
    
    
    def gettxt():
        sep=".,:;?!"
        txt=open('note.txt','r').read().lower()#打开txt文件,并进行读取,并把文章中的大写字母转换为小写,进行文件预处理
        for ch in sep:
            txt=txt.replace(ch,'')
            return txt
    
    bigList= gettxt().split()
    print('has:',bigList.count('has'))
    bigSet=set(bigList)
    bigSet=bigSet-exclude#删除无意义的词语
    print(bigSet)
    bigDict ={}
    for word in bigSet:
        bigDict[word] = bigList.count(word)#输出词语出现的频率
        print(bigDict)
        #print(bigDict.keys())
        #print(bigDict.items())
        word=list(bigDict.items())
        word.sort(key=lambda x:x[1],reverse=True)#通过词频的数量来排序,并且由大到小排序
        print(word)
    
        import pandas as pd
        pd.DataFrame(data=word).to_csv('big.csv',encoding='utf-8')#把统计词频的结果保存进名为big的csv格式的文件中


    实验运行结果:

    
    
  • 相关阅读:
    leetcode — spiral-matrix-ii
    leetcode — spiral-matrix
    leetcode — maximum-subarray
    leetcode — n-queens
    leetcode — powx-n
    leetcode — anagrams
    bzoj 2194: 快速傅立叶之二 FFT
    bzoj 4503 两个串 快速傅里叶变换FFT
    Codeforces 762D Maximum path 动态规划
    Codeforces 762C Two strings 字符串
  • 原文地址:https://www.cnblogs.com/zyd1234/p/10509664.html
Copyright © 2020-2023  润新知