程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python data types -- Dictionaries

編輯:Python

The dictionary is python The only mapping type in ( Hashtable ) And it's out of order , The dictionary object is variable , But the dictionary keys use a hash algorithm , So you have to use immutable objects , It can be obtained. , list , A dictionary cannot be used as key value , And different types of key values can be used in a dictionary .

Because the dictionary uses hash Algorithms can , Equivalent to knowing enough key The specific address of , Therefore, the time complexity of finding the value is O(1), We can get the data we want by one query .

t={'a':1,'b':2}
print(' return key A list of , You can use list The function sets the return as a list ,',list(t.keys()))
print(' return values A list of , You can use list The function sets the return as a list ,',list(t.values()))
print(' You can use list The function sets the return as a list ,',list(t.items()))
Return results :
return key A list of , You can use list The function sets the return as a list , ['a', 'b']
return values A list of , You can use list The function sets the return as a list , [1, 2]
You can use list The function sets the return as a list , [('a', 1), ('b', 2)]

newly added , modify

t={'a':1,'b':2}
# When key When there is no , Add new health value pairs
# When key In existence , to update value value
t['c'] = 3
t['b'] = 4
print(t)
Return results :
{'a': 1, 'b': 4, 'c': 3}
# fromkeys(), The elements in the batch add dictionary have the same key value ,
# aa Can be a string , Tuples , list , Default values by none
aa='ABC'
dict={}
dict = dict.fromkeys(aa)
print(dict)
dict = dict.fromkeys(aa,'zhenhai') #zhenhai As the default value
print(dict)
Return results :
{'A': None, 'B': None, 'C': None}
{'A': 'zhenhai', 'B': 'zhenhai', 'C': 'zhenhai'}

Delete

dic = {'A': '1', 'B': '2', 'C': '3'}
del dic['A'] # Delete the key value in the dictionary A The elements of
print(dic)
repop = dic.pop('B') # designated key And return to delete value value
print(dic,repop)
dic = {'A': '1', 'B': '2', 'C': '3'}
dic.popitem() # Delete the last bit and return ,3.7 Features only available after version
print(dic)
dic.clear() # Delete all elements of the dictionary
print(dic)
Return results :
{'B': '2', 'C': '3'}
{'C': '3'} 2
{'A': '1', 'B': '2'}
{}

see

dic = {'A': '1', 'B': '2', 'C': '3'}
print(' Judge key Whether the value is in the dictionary ,','A' in dic)
print(' obtain value value , Use [] Look for it , If there is no such key Will report a mistake , So we use get, If you don't find it, you will return none,',dic['A'],dic.get('B'))
for i in dic:
print(i) # Cycle get key value
for i in dic:
print(dic.get(i)) # Cycle get values
Return results :
Judge key Whether the value is in the dictionary , True
obtain value value , Use [] Look for it , If there is no such key Will report a mistake , So we use get, If you don't find it, you will return none, 1 2
A
B
C
1
2
3


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved