Students who have studied Java know that there is a data format for key-value pairs in Java--map
In Python, there is a format similar to map, which is the dictionary (Dict)
1. Create a Dictionary type Object
user={ "username":"Dylan", " Age ":", "password":"123" }
2. Traverse
User={ "username":"Dylan", " Age":" -", "Password":"123"} forKvinchUser.items ():Print(k +":"+V);
Results: Username:dylan
Age:18
Password:123
3.Clear () method--Clears all elements in the dictionary
user={ " Username ": " Dylan " " age " : " 18 " , password " : " 123 " }user.clear (); print (user);
The result is:{}
4.Copy () copies one part into another dictionary
user={ " Username ": " Dylan " " age " : " 18 " , password " : " 123 " }user2 =user.copy (); print (user2);
Result:{' password ': ' 123 ', ' username ': ' Dylan ', ' age ': ' + '}
5.Fromkeys(): Do not preserve the value of the source dictionary, create a new dictionary
user={ " Username ": " Dylan " " age " : " 18 " , password " : " 123 " }user2 =dict.fromkeys (user); print (user2);
Result:{' username ': none, ' age ': none, ' Password ': none}
6.Get(): Get the value by key
User={ "username":"Dylan", " Age":" -", "Password":"123"}username=user.get ("username");Print(username);
Results:Dylan
7. Items (): Get all the entries for the dictionary (item)
user={ " Username ": " Dylan " " age " : " 18 " , password " : " 123 " }arr =user.items (); print (arr);
Results:dict_items ([' Age ', ' + '), (' username ', ' Dylan '), (' Password ', ' 123 ')])
8.keys(): Get all keys
user={ " Username ": " Dylan " " age " : " 18 " , password " : " 123 " }arr =user.keys (); print (arr);
Results:dict_keys ([' username ', ' password ', ' age '])
9.Pop(): Removes the item item of the specified key
User={ "username":"Dylan", " Age":" -", "Password":"123"}user.pop (" Age");Print(user);
Result:{' password ': ' 123 ', ' username ': ' Dylan '}
10. Popitem ()-Removes the first item in a dictionary
user={ " Username ": " Dylan " " age " : " 18 " , password " : " 123 " }user.popitem (); print (user);
Result:{' age ': ' + ', ' password ': ' 123 '}
Items(): Get all the values of the dictionary
user={ " Username ": " Dylan " " age " : " 18 " , password " : " 123 " }values =user.values (); print (values);
Results:dict_values (['], ' Dylan ', ' 123 ')
del keyword-deletes the specified item according to the specified key
User={ "username":"Dylan", " Age":" -", "Password":"123"}deluser["username"]Print(user);
Result:{' age ': ' + ', ' password ': ' 123 '}
------------------------------------------------------End-------------------------------------------------------
I will soon fall down, but will tell me, perseverance is victory, I work until the sun to see tomorrow!
Python day two-basic use of dictionary types explained