list=['Jack','Lucy','Mary'] list.append('Pony') print("末尾增長元素 :{}".format(list)) list.insert(1,"Lili") print("指定位置增長元素 :{}".format(list)) list=['Jack','Lucy','Mary'] list.remove('Jack') print("刪除元素 :{}".format(list)) list=['Jack','Lucy','Mary'] list.pop(0) print("刪除元素 :{}".format(list)) list=['Jack','Lucy','Mary'] list[0]='a' print("修改元素 :{}".format(list)) list=['Jack','Lucy','Mary'] print("查找元素 :{}".format(list[0])) print("遍歷列表") for l in list: print("{} :{}".format(list.index(l),l))
tup=('Jack','Lucy',0) tup2=('Lili',) tup3=tup+tup2 print("鏈接/增長元素 :{}".format(tup3)) tup=('Jack','Lucy',0) print("訪問元素 :tup[2]={},tup[0:1]={}".format(tup3[2],tup[0:2])) tup=('Jack','Lucy',0) print("刪除元祖") del tup tup=('Jack','Lucy',0) print("遍歷元組:") for t in tup: print(t)
dict={'Jack':90,'Mary':80,'Tony':70} dict["abc"]=100 print("增長abc:{}".format(dict)) dict={'Jack':90,'Mary':80,'Tony':70} del dict['Jack'] print("刪除Jack:{}".format(dict)) dict={'Jack':90,'Mary':80,'Tony':70} dict.pop('Tony') print("刪除Tony:{}".format(dict)) dict={'Jack':90,'Mary':80,'Tony':70} dict['Tony']=99 print("修改Tony的值:{}".format(dict)) dict['a']=dict.pop('Jack') print("修改Tony的鍵:{}".format(dict)) dict={'Jack':90,'Mary':80,'Tony':70} print("查找Mary的值:{}".format(dict.get('Mary'))) print("遍歷字典:") for d in dict: print("{} : {}".format(d,dict[d]))
a=set('abcd') a.add('z') print("增長'z':",a) a.update({1,2}) print("增長 1,2:",a) b=set(('a','b','c')) b.remove('a') print("刪除a :",b) a=set('abcd') a.discard('b') print("刪除a :",a) a=set('Jack') a.pop() print("pop刪除 :",a) a=set('Jack') a.clear() print("清空集合 :",a) a=set('Jack') b=set(('1','2')) c=set.union(a,b) print("集合的合集",c) print("遍歷集合:") for s in c: print(s)
fo = open(r'G:\test\TheLittlePrince.txt', encoding='utf-8-sig') theLittlePrinceTxt = fo.read() txt = theLittlePrinceTxt.lower() fo.close() sep = ''' ,./:?/! '\n " [] () ~ ''' stops = {'by', 'his', 'their', 'again', 'off', 'where', 'now', 'up', 'this', 'before', 'which', 'after', 'a', 'then', "haven't", 'weren', 'll', 'down', 'or', 'no', "shan't", 'herself', 'in', 'some', 'such', "she's", 'does', 'nor', 'just', "won't", 'them', 'further', 'how', 'am', 'mightn', 'it', 'too', 'ourselves', 'is', 'couldn', 'themselves', 'should', 'ain', 'o', 'hadn', 'under', 'shan', 'him', "it's", 've', 'to', "don't", 'at', 'these', 'our', 'same', 'between', "you'd", 'isn', 'yourselves', 'until', 't', "mustn't", 'didn', 'few', 'each', 're', 'through', 'above', 'all', "you're", 'been', 'hers', 'have', 'being', 'if', 'theirs', 'most', "doesn't", "hasn't", 'an', 'and', 'below', "couldn't", 'i', 'we', "hadn't", 'mustn', 'about', "shouldn't", 'there', 'her', 'y', 'here', 'was', "isn't", "needn't", 'were', 'haven', 'out', 'ours', 'over', 'once', 'having', 'against', 'don', 'has', 'but', 'wouldn', 'with', 'other', 'doesn', 'itself', 'aren', 'when', 'as', "wasn't", 'myself', "you'll", 'because', 'the', 'so', "didn't", 'are', 'for', "you've", 'd', 'hasn', 'wasn', 'on', 'he', 's', 'of', 'they', 'needn', 'ma', 'while', 'than', 'from', "weren't", 'those', 'what', 'who', 'himself', "should've", 'will', 'whom', 'more', "mightn't", 'do', 'its', 'why', 'only', 'that', 'during', 'not', 'had', 'own', 'm', 'me', 'very', 'doing', 'can', 'be', 'my', 'both', 'into', "aren't", 'shouldn', 'won', 'yours', 'did', 'you', 'she', 'yourself', 'your', "wouldn't", 'any', "that'll"} # 定義停用詞 for s in sep: txt = txt.replace(s, " ") allWord = txt.split() mset = set(allWord) # 去掉重複的單詞,將文本轉換爲集合 mset = mset - stops # 去除停用詞 mdict = {} # 定義字典,用於輸出 for m in mset: mdict[m] = allWord.count(m) # 統計每一個字典的key的頻數 mlist = list(mdict.items()) # 字典轉換成列表 mlist.sort(key=lambda x: x[1], reverse=True) # 列表排序 print(mlist[0:10]) print(mlist[10:20]) #輸出top20 import pandas as pd # 在默認目錄生成csv文件 pd.DataFrame(data=mlist).to_csv('Little.csv', encoding='utf-8')