1 字典
特點
1.鍵-值成對出現 “鍵:值 ”
2.鍵不能重復
3.鍵不可更改,值可以修改
4.鍵來索引值
5.鍵只能是不可變的數據類型
dic_Python={'the define':1,'the age':2,'the clude':['A','B']
}
#增刪查改
dic_Python['the age']
dic_Python['the define']=77#賦值修改
dic_Python['hello']=50#賦值添加鍵值對
dic_Python.update({'hi':'world',})del dic_Python['the age']#刪除指定鍵值對dic_Python.keys()#訪問字典所有鍵
dic_Python.values()#訪問字典所有值
dic_Python.items()#訪問字典所有鍵值對third_dic={i:i**3 for i in range(1,11)}
2 統計單詞詞頻
常用方法
1.字符串.lower()#將所有字母轉為小寫形式
2.字符串.split()#將字符串拆分 返回列表,包含一系列單詞
?
sentence_1='Many flowers and trees have withered this night;Death will soon come and plant them over again.'
sentence_1.lower()
words=sentence_1.split()
word_num={}
for word in words:if word in word_num:word_num[word]+=1else:word_num[word]=1