列表的增删改查及遍历
#列表 list=[‘Hello‘,‘World‘] print(list) #增加 list.append(‘!‘) print(list) list.insert(1,"My") print(list) #删除 list.pop(1) print(list) #修改 list[1]=‘Python‘ print(list) #查找 print(list[1]) #遍历 for i in list: print(i)
元组的查找以及遍历
#元组 tup=(‘Hello‘, ‘World‘) print(tup) #查找 print(tup[1]) #遍历 for i in tup: print(i)
字典的增删改查及遍历
#字典 dict={‘Panda‘:12,‘Dog‘:20} print(dict) #增加 dict[‘Cat‘]=16 print(dict) #删除 dict.pop("Cat") print(dict) #修改 dict[‘Dog‘]=12 print(dict) #查找 print("Dog:",dict[‘Dog‘]) #遍历 for i in dict: print(i)
集合的增删以及遍历
#集合 set={"Baidu","TaoBao"} print(set) #增加 set.add("YaHu") print(set) #删除 set.remove("YaHu") print(set) #遍历 for i in set: print(i)
总结列表,元组,字典,集合的联系与区别
列表 | 字典 | 元组 | 集合 | |
括号 | [] | {} | () | {} |
有序无序 |
有序 | 有序 | 有序 | 无序 |
可变不可变 | 可变 | 可变 | 不可变 | 可变 |
重复不可重复 | 可重复 | 不可重复 | 可重复 | 不可重复 |
存储与查找方式 | 索引 | 键值 | 索引 | 无 |
词频统计
import pandas as pd import stopwords stopwords=stopwords.get_stopwords(‘english‘) f=open("Crimes and Punishments.txt","r",encoding="utf-8") str=f.read() f.close() remove=",,—-。?.()?!‘’" str=str.lower() dict={} for i in remove: str=str.replace(i," ") list=str.split() for word in list: if word not in stopwords: dict[word]=list.count(word) d=sorted(dict.items(), reverse=True, key=lambda d:d[1]) for l in range(20): print(d[l][0],"--",d[l][1]) pd.DataFrame(data=d).to_csv(‘words.csv‘,encoding=‘utf-8‘)
可视化词云
原文:https://www.cnblogs.com/pybblog/p/10530082.html