組合數據類型和英文詞頻統計實例
阿新 • • 發佈:2017-09-20
think laughing clas self why rms day index oom
1.列表實例:由字符串創建一個作業評分列表,做增刪改查詢統計遍歷操作。例如,查詢第一個3分的下標,統計1分的同學有多少個,3分的同學有多少個等。
>>> ls=list(‘1231323232323131323‘) >>> ls [‘1‘, ‘2‘, ‘3‘, ‘1‘, ‘3‘, ‘2‘, ‘3‘, ‘2‘, ‘3‘, ‘2‘, ‘3‘, ‘2‘, ‘3‘, ‘1‘, ‘3‘, ‘1‘, ‘3‘, ‘2‘, ‘3‘] >>> ls.append(‘4‘) >>> ls.append(‘5‘) >>> ls [‘1‘, ‘2‘, ‘3‘, ‘1‘, ‘3‘, ‘2‘, ‘3‘, ‘2‘, ‘3‘, ‘2‘, ‘3‘, ‘2‘, ‘3‘, ‘1‘, ‘3‘, ‘1‘, ‘3‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘] >>> ls.pop() ‘5‘ >>> ls [‘1‘, ‘2‘, ‘3‘, ‘1‘, ‘3‘, ‘2‘, ‘3‘, ‘2‘, ‘3‘, ‘2‘, ‘3‘, ‘2‘, ‘3‘, ‘1‘, ‘3‘, ‘1‘, ‘3‘, ‘2‘, ‘3‘, ‘4‘] >>> ls.index(‘3‘) 2 >>> ls.count(‘1‘) 4 >>> ls.count(‘3‘) 9 >>>
2.字典實例:建立學生學號成績字典,做增刪改查遍歷操作。
>>> d={‘01‘:‘95‘,‘02‘:‘85‘,‘03‘:‘90‘,‘04‘:‘92‘,‘05‘:‘72‘,‘06‘:‘93‘} >>> d.keys() dict_keys([‘01‘, ‘02‘, ‘03‘, ‘04‘, ‘05‘, ‘06‘]) >>> d.values() dict_values([‘95‘, ‘85‘, ‘90‘, ‘92‘, ‘72‘, ‘93‘]) >>> d.get(‘03‘,‘100‘) ‘90‘ >>> d[‘99‘]=‘99‘ >>> d {‘01‘: ‘95‘, ‘02‘: ‘85‘, ‘03‘: ‘90‘, ‘04‘: ‘92‘, ‘05‘: ‘72‘, ‘06‘: ‘93‘, ‘99‘: ‘99‘}
3.列表,元組,字典,集合的遍歷。總結列表,元組,字典,集合的聯系與區別。
>>> l=list(‘123456665432145‘) >>> t=tuple(‘65432566123333‘) >>> d={‘01‘:‘劉‘,‘02‘:‘張‘,‘03‘:‘王‘,‘04‘:‘李‘} >>> s=set(‘123654598777296582‘) >>> l [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘6‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘, ‘1‘, ‘4‘, ‘5‘] >>> >>> t (‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘, ‘5‘, ‘6‘, ‘6‘, ‘1‘, ‘2‘, ‘3‘, ‘3‘, ‘3‘, ‘3‘) >>> >>> d {‘01‘: ‘劉‘, ‘02‘: ‘張‘, ‘03‘: ‘王‘, ‘04‘: ‘李‘} >>> >>> s {‘8‘, ‘2‘, ‘7‘, ‘9‘, ‘1‘, ‘3‘, ‘6‘, ‘5‘, ‘4‘} >>>
列表使用方括號,元組使用小括號,字典使用花括號,集合使用[()];
元組創建很簡單,只需要在括號中添加元素,並使用逗號隔開即可,列表則復雜多;
列表、字典、集合能增加、修改、刪除,元組不能;
4.英文詞頻統計實例
new = ‘‘‘An empty street An empty house A hole inside heart all alone and the rooms Are getting smaller I wonder how I wonder why I wonder where they are The days we had The songs we sang together And oh! my love holding on forever Reaching for a love That seems so far So I say a litter prayer No my dream will take me there Where the skies are blue to see you once again my love Overseas from coast to coast Find a place I love the most Where the fields are green To see you once again My love I Try to read I go to work laughing with my friends But I can‘t stop to keep myself From thinking I wonder how I wonder why I wonder where they are The days we had The songs we sang togetther And oh! my love I‘mholding on forever Reaching for a love That seems so far So I say a litter prayer No my dream will take me there Where the skies are blue to see you once again my love Overseas from coast to coast Find a place I love the most Where the fields are green To see you once again To hold you in my arms To promise my love To tell you from my heart You are all I‘m thinking of Reaching for a love That seems so far So I say a litter prayer No my dream will take me there Where the skies are blue to see you once again my love Overseas from coast to coast Find a place I love the most Where the fields are green To see you once again My love And hope my dream will take me there Where the skies are blue to see you once again my love Overseas from coast to coast Find a place I love the most Where the fields are green To see you once again My love‘‘‘ new=new.lower() for i in ‘.,"‘: new=new.replace(i,‘ ‘) word = new.split(‘ ‘) dic ={} keys = set(word) for i in keys: dic[i]=word.count(i) words = list(dic.items()) words.sort(key= lambda x:x[1],reverse = True) for i in range(10): print(word[i])
組合數據類型和英文詞頻統計實例