1. 程式人生 > >課堂筆記:Python基礎-字典

課堂筆記:Python基礎-字典

更新 tabs with numeric ide rfi form pda []

Python字典的兩大特點:無序、鍵唯一

#字典創建
dic={name:alex} #第一種形式 dic2=dict(((name,alex),)) #第二種形式 print(dic2) dic3=dict([[name,alex],]) #第三種形式 print(dic3) # dic1={‘name‘:‘alex‘} # dic1[‘age‘]=18 # print(dic1) #鍵存在,不改動,返回字典中相應的鍵對應的值 # ret=dic1.setdefault(‘age‘,34) # print(ret) # # #鍵不存在,在字典中中增加新的鍵值對,並返回相應的值
# ret2=dic1.setdefault(‘hobby‘,‘girl‘) # print(dic1) # print(ret2) #查 通過鍵去查找 # dic3={‘age‘: 18, ‘name‘: ‘alex‘, ‘hobby‘: ‘girl‘} # # print(dic3[‘name‘]) # # print(list(dic3.keys()))  #輸出所有鍵 # print(list(dic3.values()))  #輸出所有鍵值 # print(list(dic3.items()))  #輸出所有鍵值 #字典修改 # li=[1,2,34,4] # li[2]=5 #
dic3={‘age‘: 18, ‘name‘: ‘alex‘, ‘hobby‘: ‘girl‘} # dic3[‘age‘]=55 # print(dic3) # dic4={‘age‘: 18, ‘name‘: ‘alex‘, ‘hobby‘: ‘girl‘} # # dic5={‘1‘:‘111‘,‘2‘:‘222‘} # dic5={‘1‘:‘111‘,‘name‘:‘222‘} # # dic4.update(dic5) # print(dic4) # print(dic5) # dic5 = {‘name‘: ‘alex‘, ‘age‘: 18, ‘class‘: 1} #
dic5.clear() # 清空字典 # print(dic5) # del dic5[‘name‘] #刪除字典中指定鍵值對 # print(dic5) # print(dic5.pop(‘age‘)) #刪除字典中指定鍵值對,並返回該鍵值對的值 # ret=dic5.pop(‘age‘) # print(ret) # print(dic5) # a = dic5.popitem() #隨機刪除某組鍵值對,並以元組方式返回值 # print(a, dic5) # del dic5 #刪除整個字典 # print(dic5) #5 其他操作以及涉及到的方法 # dic6=dict.fromkeys([‘host1‘,‘host2‘,‘host3‘],‘test‘) # print(dic6)#{‘host3‘: ‘test‘, ‘host1‘: ‘test‘, ‘host2‘: ‘test‘} # # dic6[‘host2‘]=‘abc‘ # print(dic6) # dic6=dict.fromkeys([‘host1‘,‘host2‘,‘host3‘],[‘test1‘,‘tets2‘]) # print(dic6)#{‘host2‘: [‘test1‘, ‘tets2‘], ‘host3‘: [‘test1‘, ‘tets2‘], ‘host1‘: [‘test1‘, ‘tets2‘]} # # dic6[‘host2‘][1]=‘test3‘ # print(dic6)#{‘host3‘: [‘test1‘, ‘test3‘], ‘host2‘: [‘test1‘, ‘test3‘], ‘host1‘: [‘test1‘, ‘test3‘]} # av_catalog = { # "歐美":{ # "www.youporn.com": ["很多免費的,世界最大的","質量一般"], # "www.pornhub.com": ["很多免費的,也很大","質量比yourporn高點"], # "letmedothistoyou.com": ["多是自拍,高質量圖片很多","資源不多,更新慢"], # "x-art.com":["質量很高,真的很高","全部收費,屌比請繞過"] # }, # "日韓":{ # "tokyo-hot":["質量怎樣不清楚,個人已經不喜歡日韓範了","聽說是收費的"] # }, # "大陸":{ # "1024":["全部免費,真好,好人一生平安","服務器在國外,慢"] # } # } # av_catalog[‘歐美‘]["www.youporn.com"][1]=‘高清午馬‘ dic={5:555,2:666,4:444} # dic.has_keys(5) # print(5 in dic) # print(sorted(dic.items())) # dic5={‘name‘: ‘alex‘, ‘age‘: 18} # for i in dic5: # print(i,dic5[i])+ # for i,v in dic5.items(): # print(i,v) #String 操作 # a="Let‘s go " # print(a) # 1 * 重復輸出字符串 # print(‘hello‘*20) # 2 [] ,[:] 通過索引獲取字符串中字符,這裏和列表的切片操作是相同的,具體內容見列表 # print(‘helloworld‘[2:]) #關鍵字 in # print(123 in [23,45,123]) # print(‘e2l‘ in ‘hello‘) # 4 % 格式字符串 # print(‘alex is a good teacher‘) # print(‘%s is a good teacher‘%‘alex‘) #5 # a=‘123‘ # b=‘abc‘ # d=‘44‘ # # # c=a+b # # # print(c) # # # c= ‘‘.join([a,b,d]) # print(c) # 字符串的內置方法 # st=‘hello kitty {name} is {age}‘ # # print(st.count(‘l‘)) # 統計元素個數 # print(st.capitalize()) # 首字母大寫 # print(st.center(50,‘#‘)) # 居中 # print(st.endswith(‘tty3‘)) # 判斷是否以某個內容結尾 # print(st.startswith(‘he‘)) # 判斷是否以某個內容開頭 # print(st.expandtabs(tabsize=20)) # print(st.find(‘t‘)) # 查找到第一個元素,並將索引值返回 # print(st.format(name=‘alex‘,age=37)) # 格式化輸出的另一種方式 待定:?:{} # print(st.format_map({‘name‘:‘alex‘,‘age‘:22})) # print(st.index(‘t‘)) # print(‘asd‘.isalnum()) # print(‘12632178‘.isdecimal()) # print(‘1269999.uuuu‘.isnumeric()) # print(‘abc‘.isidentifier()) # print(‘Abc‘.islower()) # print(‘ABC‘.isupper()) # print(‘ e‘.isspace()) # print(‘My title‘.istitle()) # print(‘My tLtle‘.lower()) # print(‘My tLtle‘.upper()) # print(‘My tLtle‘.swapcase()) # print(‘My tLtle‘.ljust(50,‘*‘)) # print(‘My tLtle‘.rjust(50,‘*‘)) # print(‘\tMy tLtle\n‘.strip()) # print(‘\tMy tLtle\n‘.lstrip()) # print(‘\tMy tLtle\n‘.rstrip()) # print(‘ok‘) # print(‘My title title‘.replace(‘itle‘,‘lesson‘,1)) # print(‘My title title‘.rfind(‘t‘)) # print(‘My title title‘.split(‘i‘,1)) # print(‘My title title‘.title()) #摘一些重要的字符串方法 #1 print(st.count(‘l‘)) # print(st.center(50,‘#‘)) # 居中 # print(st.startswith(‘he‘)) # 判斷是否以某個內容開頭 # print(st.find(‘t‘)) # print(st.format(name=‘alex‘,age=37)) # 格式化輸出的另一種方式 待定:?:{} # print(‘My tLtle‘.lower()) # print(‘My tLtle‘.upper()) # print(‘\tMy tLtle\n‘.strip()) # print(‘My title title‘.replace(‘itle‘,‘lesson‘,1)) # print(‘My title title‘.split(‘i‘,1))

課堂筆記:Python基礎-字典