Python語言學(xué)習(xí)之列表、元祖、字典那些事:python和列表、元祖、字典的使用方法之詳細(xì)攻略 目錄 列表、元祖、字典那些事 0、創(chuàng)建空列表 1、列表統(tǒng)計、循環(huán)、代替 2、創(chuàng)建多個空列表的方法及其應(yīng)用范圍 3、Python查找列表中的重復(fù)元素那些事 4、利用for循環(huán)依次輸出的單條信息存在一個列表內(nèi) 5、對字典進(jìn)行排序
列表、元祖、字典那些事0、創(chuàng)建空列表citys_list=[[]]*len(dup_list) #出了很多錯誤,append不能向列表內(nèi)列表添加元素 citys_tuple=(tuple(citys_list)) #輸出([], [], [], [])但是不同于直接創(chuàng)建元祖citys_tuple=([],[],[],[])
1、列表統(tǒng)計、循環(huán)、代替#對列表內(nèi)元素各統(tǒng)計個數(shù),并生成字典,然后分別取出keys、values各組成列表 list=['America', 'America', '山東', '山東', '吉林', '山東', '上海', '上海'] print(list.count('山東')) #計算列表指定元素的個數(shù) list_dict[i] = list.count(i) print(keys) #但是,輸出的是dict_keys(['America', '山東', '吉林', '上海']) #for循環(huán)遍歷字典分別取出keys、values各組成列表 for key in list_dict.keys(): #for循環(huán)遍歷字典的keys、values for value in list_dict.values(): #列表里的元素進(jìn)行替換,使用列表解析的方法 #替換列表中的某個元素,輸出新列表只能替換一次,不能for循環(huán)替換,因為x不是個循環(huán)的變量 newlst = ['4' if x == '1' else x for x in lst]
2、創(chuàng)建多個空列表的方法及其應(yīng)用范圍#創(chuàng)建多個空列表的方法及其應(yīng)用范圍 lists01=([],[],[],[]) ##T1、此為創(chuàng)建已知個數(shù)的空列表,同時滿足向大列表中元素小列表添加元素時,不保證同步。 lists02=[[]]*4 ##T2、魔法乘法不合適,如list1=[[]]*n,這種方式不能滿足append的方法 def create_empty_lists(num): lists =create_empty_lists(5)
3、Python查找列表中的重復(fù)元素那些事(1)、實現(xiàn)查找列表中重復(fù)元素 dict2list=['汕頭', '杭州', '周口', '廣元', '佛山', '佛山', '上海', '寧波', '東莞', '臺州', '仙桃', '陽泉', '安慶', '泉州', '黃岡', '宿遷', '黃岡', '常州', '杭州', '潮州', '東陽', '淮安', '重慶', '廊坊', '威海', '黃石', '北京', '成都', '保定', '上海', '佛山', '紹興'] if dict2list.count(i)>=2:
list2=list1.copy() #為了不破壞原數(shù)據(jù),臨時淺復(fù)制給變量list2 list3=list(set(list2)) #使用set()函數(shù)將list2轉(zhuǎn)為集合去掉重復(fù)元素又用list()轉(zhuǎn)回列表 for i in list3: #對無重復(fù)元素的列表list3迭代 list1.remove(i) #當(dāng)list3內(nèi)的元素存在于list2中,則把其元素從list2中刪除,最后就會留下重復(fù)元素 print('list1:%s中的重復(fù)元素有:%s'%(list1,list2)) #組后輸出原數(shù)據(jù)list1和最終結(jié)果list2 list1:['佛山', '黃岡', '杭州', '上海', '佛山']中的重復(fù)元素有:['汕頭', '杭州', '周口', '廣元', '佛山', '佛山', '上海', '寧波', '東莞', '臺州', '仙桃', '陽泉', '安慶', '泉州', '黃岡', '宿遷', '黃岡', '常州', '杭州', '潮州', '東陽', '淮安', '重慶', '廊坊', '威海', '黃石', '北京', '成都', '保定', '上海', '佛山', '紹興']
(2)、查找列表內(nèi)每個元素、每個元素對應(yīng)重復(fù)次數(shù) #T3、查找列表內(nèi)每個元素、每個元素對應(yīng)重復(fù)次數(shù) print(each_b, ': ', count)
(3)、查找重復(fù)元素并輸出重復(fù)的元素的索引值 #T4、查找重復(fù)元素并輸出重復(fù)的元素的索引值 from collections import defaultdict def list_duplicates(seq): tally = defaultdict(list) for i,item in enumerate(seq): return ( (key,locs) for key,locs in tally.items() for dup in sorted(list_duplicates(source)):
(4)、比較并輸出兩個列表中的相同元素和不同元素 list1 = ['筆記本電腦 ', '手提包 ', '人類 ', '人類 '] list2 = ['筆記本電腦 ', '手機(jī)','手提包 ', '人類 ', '人類 '] same_element = [x for x in list1 if x in list2] different_element = [y for y in (list1+list2) if y not in same_element] ['筆記本電腦 ', '手提包 ', '人類 ', '人類 ']
4、利用for循環(huán)依次輸出的單條信息存在一個列表內(nèi)import os #將for循環(huán)依次輸出的單條信息存在一個列表內(nèi) path ='F:\\File_Python\\Python_example\\faceRecognition-master\\Original_picture\\Jason_niu' for fpathe,dirs,fs in os.walk(path):

5、對字典進(jìn)行排序#字典排序:將兩個列表組成字典內(nèi)的keys、values進(jìn)行排序 x = ['LiR', 'RFR', 'ETR', 'SGDR', 'GBR', 'LGB'] model_score = [0.6, 0.7, 0.4, 0.8, 0.1, 0.5] accuracy_dict = dict(zip(x,model_score)) dict_sort_by_key=sorted(accuracy_dict.items()) #按照key排序 dict_sort_by_value=sorted(accuracy_dict.items(),key=lambda x: x[1],reverse=False) #按照value排序 print(dict_sort_by_value) dict_sort_by_value_keys = dict(dict_sort_by_value).keys() dict_sort_by_value_values = dict(dict_sort_by_value).values() print(list(dict_sort_by_value_keys) ) print(list(dict_sort_by_value_values) ) [('ETR', 0.4), ('GBR', 0.1), ('LGB', 0.5), ('LiR', 0.6), ('RFR', 0.7), ('SGDR', 0.8)] [('GBR', 0.1), ('ETR', 0.4), ('LGB', 0.5), ('LiR', 0.6), ('RFR', 0.7), ('SGDR', 0.8)] ['GBR', 'ETR', 'LGB', 'LiR', 'RFR', 'SGDR'] [0.1, 0.4, 0.5, 0.6, 0.7, 0.8]
|