1. 程式人生 > >9.22

9.22

大小 spa all down 內容 not 聯系 通過 分析字符串

1.字典實例:建立學生學號成績字典,做增刪改查遍歷操作。

#字典實例:建立學生學號成績字典,做增刪改查遍歷操作。
cj={39:89,33:90,38:80,40:79}
cj[36]=78#增加
print(cj)
cj.pop(39)#刪除
print(cj)
cj[40]=69#修改值
print(cj)
print(cj.get(33,不存在))#查找,存在
print(cj.get(45,不存在))#查找,不存在

技術分享

2.列表,元組,字典,集合的遍歷。

#列表,元組,字典,集合的遍歷。
ls=list(2376527)
print(遍歷列表:
) for i in ls: print(i) tp=tuple(76352526) print(遍歷元組:) for i in tp: print(i) dt={a:1,b:2,c:3,d:4} print(遍歷字典:) for i in dt: print(i) s=set(3242324) print(遍歷集合:) for i in s: print(i)

技術分享

3.總結列表,元組,字典,集合的聯系與區別。

1.列表,元組,字典是有順序的,而集合是沒順序的

2.列表是以方括號形式表示,元組是以圓括號表示,字典以花括號表示,集合則是以[()]的形式表示

3.列表是可變對象,它支持在原處修改的操作.也可以通過指定的索引和分片獲取元素。區別於元組,可動態增加,刪除,更新。

4.元組和列表在結構上沒有什麽區別,唯一的差異在於元組是只讀的,不能修改。元組用“()”表示。元組一旦定義其長度和內容都是固定的。一旦創建元組,則這個元組就不能被修改,即不能對元組進行更新、增加、刪除操作。若想創建包含一個元素的元組,則必須在該元素後面加逗號“,”,否則創建的不是一個元組,而是一個字符串。

5.集合沒有特殊的表示方法,而是通過一個set函數轉換成集合。集合是一個無序不重復元素集,基本功能包括關系測試和消除重復元素.。

6.字典最大的價值是查詢,通過鍵,查找值。

4.英文詞頻統計實例

待分析字符串

分解提取單詞

大小寫 txt.lower()

分隔符‘.,:;?!-_’

單詞列表

單詞計數字典

sorry=‘‘‘You gotta go and get angry at all of my honesty
You know I try but I don’t do too well with apologies
I hope I don’t run out of time, could someone call a referee?
Cause I just need one more shot at forgiveness
I know you know that I made those mistakes maybe once or twice
By once or twice I mean maybe a couple a hundred times
So let me, oh let me redeem, oh redeem, oh myself tonight
Cause I just need one more shot at second chances
Yeah, is it too late now to say sorry?
Cause I’m missing more than just your body
Is it too late now to say sorry?
Yeah I know that I let you down
Is it too late to say I’m sorry now?
I’m sorry, yeah
Sorry, yeah
Sorry
Yeah I know that I let you down
Is it too late to say sorry now?
I’ll take every single piece of the blame if you want me to
But you know that there is no innocent one in this game for two
I’ll go, I’ll go and then you go, you go out and spill the truth
Can we both say the words and forget this?
Is it too late now to say sorry?
Cause I’m missing more than just your body
Is it too late now to say sorry?
Yeah I know that I let you down
Is it too late to say I’m sorry now?
I’m not just trying to get you back on me
Cause I’m missing more than just your body
Is it too late now to say sorry?
Yeah I know that I let you down
Is it too late to say sorry now?
I’m sorry, yeah
Sorry, oh
Sorry
Yeah I know that I let you down
Is it too late to say sorry now?
I’m sorry, yeah
Sorry, oh
Sorry
Yeah I know that I let you down
Is it too late to say sorry now?‘‘‘
sorry=sorry.lower()#首字母小寫
for i in ,?:
    sorry=sorry.replace(i, )#替換所有的,?
word=sorry.split( )#以‘ ’斷開分成單獨的字符串
#單詞計數字典
dt={}#定義一個空字典
keys=set(word)#取鍵值
for i in keys:
    dt[i]=word.count(i)#dt[i]輸出key
print(dt)

9.22