Python筆記-7 字典
# # 列表是有序的對(duì)象集合,可以用角標(biāo)獲取list中的元素
# 可以用角標(biāo)獲取list中的元素
# brand=['李寧','鴻星爾克','361度']
# slogan=['一切皆有可能','TO BE No.1','多一度熱愛(ài)']
# print(brand[0])
# print(slogan[2])
# # 元組是有序的
# t1 = (1,2,'3')
# print (t1[1])
# 集合是無(wú)序的,不支持索引操作的
# sit1 = {1,2,6,'a',1,2}
# print (sit1[0])
# brand=['李寧','鴻星爾克','361度']? #作為key
# slogan=['一切皆有可能','TO BE No.1','多一度熱愛(ài)'] #作為value
字典的每個(gè)鍵值?key=>value?對(duì)用冒號(hào)?:?分割,每個(gè)對(duì)之間用逗號(hào)(,)分割,整個(gè)字典包括在花括號(hào)?{}?中
鍵必須是唯一的,但值則不必。
值可以取任何數(shù)據(jù)類型,但鍵必須是不可變的,如字符串,數(shù)字。
# dict={'李寧': '一切皆有可能', '鴻星爾克': 'TO BE No.1','361度':'多一度熱愛(ài)'}
# print("李寧的slogan是: ", dict['李寧'])
#
# ####################################################
#
#
# ############創(chuàng)建方式及注意點(diǎn)#############
# 創(chuàng)建字典? 字典用花括號(hào)({ })表示
# people_01 = {'Name': '小明', 'Age': 3}
#
# # 可以定義空字典
# people ={}
# print(type(people))
#
# #也可以用dict函數(shù)通過(guò)關(guān)鍵字的參數(shù)來(lái)創(chuàng)建字典:
# d = dict(name='wzw', number=22)
# print(d)
#
# people_01 = {'Name': '小明', 'Age': 3, 'Name': '菜鳥(niǎo)'} #唯一性
# print ("這個(gè)人的名字: ", people_01['Name'])
# people_02 = {['下雨', '晴天']: '小明', 'Age': 3, 'Name': '菜鳥(niǎo)'} #不可變
#
# # 字典里嵌套列表
# tem = {'北京': [22, '多云'], '上海': [23, '晴天'], '深圳': [23, '小雨'], '廣州': [23, '陰天']}
# print(tem)
#
# # 打印上海天氣整體情況
# print(tem['上海'])
#
# # 打印上海最高溫度
# print(tem['上海'][0])
############基本操作和內(nèi)置函數(shù)#############
# # 訪問(wèn)字典里的值? 字典中訪問(wèn)值:字典名+方括號(hào)+方括號(hào)內(nèi)填要取的值的鍵。 注意和get方法的區(qū)別
# people_01 = {'Name': '小明', 'Age': 3, 'Name': '菜鳥(niǎo)'}
# print ("這個(gè)人的名字: ", people_01['Name'])
# #print ("這個(gè)人的年齡: ", people_01['Country'])
# # get是個(gè)更寬松的訪問(wèn)字典項(xiàng)的方法,當(dāng)get訪問(wèn)一個(gè)不存在的鍵時(shí),不會(huì)報(bào)錯(cuò)而會(huì)得到None值
# print(people_01.get('Name'))
# print(people_01.get('Country'))
# 修改字典
# people_01 = {'Name': '小明', 'Age': 3}
# people_01['Age'] = 7 # 更新
# people_01['Country'] = "China" # 添加? 前提是字典中不存在相同的鍵,如果存在相同的鍵,則為覆蓋(同上)值操作。 = update() 方法
# print(people_01)
# print ("這個(gè)人的年齡: ", people_01['Age'])
# print ("這個(gè)人來(lái)自: ", people_01['Country'])
# # update() 方法向字典插入指定的項(xiàng)目
# car = {'brand': '寶馬','model': '911','year': 1963}
# car.update({'color': 'White'})
# print(car)
# # #還可以用該方法合并兩個(gè)字典
# car = {'brand': '寶馬','model': '911','year': 1963}
# car1 = {'color': 'White','prince': '100萬(wàn)','year': 2021}
# car.update(car1)
# print(car)
# #len()方法返回字典中鍵—值對(duì)的數(shù)量
# print (len(car))
#
# # 檢查字典中中是否有含有某鍵的項(xiàng)? key in 字典名
#
# people_01 = {'Name': '小明', 'Age': 3,'num':[1,2,3]}
# print('Age' in people_01)
# print('Country' in people_01)
# #del 刪除字典元素
# people_01 = {'Name': '小明', 'Age': 3,'num':[1,2,3]}
# del people_01['Age'] #刪除鍵值對(duì)使用 del 語(yǔ)句,這樣會(huì)永久刪除對(duì)應(yīng)的鍵值對(duì)
# print(people_01)
# print('Age' in people_01)
# # pop? pop(key[,default])
# # 注意del 語(yǔ)句和 pop() 函數(shù)作用相同,pop() 函數(shù)有返回值,返回刪除的key對(duì)應(yīng)的value。
# # default: 如果沒(méi)有 key,返回 default 值
#
# people_01 = {'Name': '小明', 'Age': 3,'num':[1,2,3]}
# people_01.pop('Age')
# #pop_obj=people_01.pop('Age')
# print(people_01)
# print('Age' in people_01)
# #print(pop_obj)
# #print(people_01.pop(('Cou'),'本操作刪除的key不存在'))
# #內(nèi)置的str()函數(shù)可以將指定的值轉(zhuǎn)換為字符串
# people_01 = {'Name': '小明', 'Age': 3}
# people_0s =str(people_01)
# print(people_0s)
# print(type(people_0s))
# print(type(people_01))
#####################################################
# 遍歷字典
#
# #? 1、遍歷所有的鍵-值對(duì) 使用函數(shù) items():?
dict.items() 把字典中每對(duì) key 和 value 組成一個(gè)元組,并把這些元組放在列表中返回?
# print(people_01.items())
# people_01 = {'Name': '小明', 'Age': 3, 'Country': 'China','num':[1,2,3]}
# for key,value in people_01.items():
#???? print (key, value)
# #? 2、遍歷字典中的所有鍵? 使用函數(shù)?? keys():? #print(people_01.keys())
# people_01 = {'Name': '小明', 'Age': 3, 'Country': 'China','num':[1,2,3]}
# for key in people_01.keys():
#???? print(key)
# #? 3、遍歷了字典中的所有鍵,那肯定可以拿到所有的值了,? 使用鍵獲取值的方法 字典名[鍵名]: 效果同1
# people_01 = {'Name': '小明', 'Age': 3, 'Country': 'China','num':[1,2,3]}
# for key in people_01.keys():
#???? print("\n"+key.title())
#???? print(people_01[key])
# #? 4、遍歷字典中的所有值? 使用函數(shù)?? values(): 以列表形式返回字典中的所有值。
# people_01 = {'Name': '小明', 'Age': 3, 'Country': 'China','num':[1,2,3]}
# print (people_01.values())
#? 5、使用函數(shù)?? sorted()按key值對(duì)字典排序:
# people_01 = {'5': '小明', '3': 3, 'A': 'China','n':[1,2,3], 'F': 'N'}
# print(sorted(people_01.keys()))
# print(sorted(people_01.keys(),reverse=True))
#
# # for key in sorted(people_01.keys()):
# #???? print("\n"+key.title())
#################其他操作##################
# #clear()方法, dict.clear() 該函數(shù)沒(méi)有任何返回值
# people_01 = {'Name': '小明', 'Age': 3,'num':[1,2,3]}
# print ("開(kāi)始的鍵值對(duì)數(shù)量 : %d" %? len(people_01))
# people_01.clear()
# print ("clear后的數(shù)量 : %d" %? len(people_01))
# print(people_01)
#copy()方法, dict.copy()
# people_01 = {'Name': '小明', 'Age': 3,'num':[1,2,3]}
# people_02 = people_01.copy()
# print(people_02)
##注意:直接賦值和 copy 的區(qū)別
# people_01 = {'Name': '小明', 'Age': 3,'num':[1,2,3]}
# people_02 = people_01.copy()? # 淺拷貝:深拷貝父對(duì)象(一級(jí)目錄),子對(duì)象(二級(jí)目錄)不拷貝,還是引用
# people_03 = people_01? #? 直接賦值 引用對(duì)象
# # 修改 data 數(shù)據(jù)
# people_01['user'] = 'root' #增加key
# people_01['num'].remove(1)?? #修改value
#
# # 輸出結(jié)果
# print(people_01)
# print(people_02)
# print(people_03) # people_01的引用(別名),所以輸出結(jié)果都是一致的
###fromkeys()方法創(chuàng)建字典
###dict.fromkeys(seq[, value])
###參數(shù)? seq -- 字典鍵值列表。?? value -- 可選參數(shù), 設(shè)置鍵序列(seq)的值。
dict={}
dict1 = dict.fromkeys(('Google', 'jingdong', 'Taobao'), 100) #value可不寫
print(dict1)
dict.fromkeys((1,2,3),'number')# 注意只用來(lái)創(chuàng)建新字典,不負(fù)責(zé)保存。當(dāng)通過(guò)一個(gè)字典來(lái)調(diào)用 fromkeys 方法時(shí),如果需要后續(xù)使用一定記得給他復(fù)制給其他的變量。
print(dict)
dict2=dict.fromkeys((1,2,3),'number')
print(dict2)
# 練習(xí):
#userinfo = {'Andy':'111','Ellis':'222','Heywood':'333'}
# #模擬用戶登錄判斷
# userinfo = {'Andy':'111','Ellis':'222','Heywood':'333'}
#
# username = input('請(qǐng)輸入用戶名:')
# password = input('請(qǐng)輸入密碼:')
#
# #判斷用戶輸入的登錄信息,先判斷用戶輸入的username是否在字典中(key(用戶名)是否在字典里)并且判斷字典中key所對(duì)應(yīng)的value是否和用戶輸入的password相等。
# if username in userinfo and userinfo[username] == password:
#???? print('登錄成功!')
# else:
#???? print('登錄失?。?#39;)
標(biāo)簽: