1, the dictionary
It helps users to information indicating a thing (thing that have multiple attributes)
Another variable is the dictionary container model, and can store any type of object. (Random arrangement)
Each dictionary key (key => value) of the colon ( : ) is divided, with between each pair comma ( , ) is divided, in the entire dictionary including curly braces ( {}) in the following format:
d = {key1 : value1, key2 : value2 }
Key must be unique, but the value is not necessary.
Value may take any data type, but the key must be immutable, such as strings, numbers, or tuples.
A simple example dictionary:
dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
So also create a dictionary:
dict1 = { 'abc': 456 } dict2 = { 'abc': 123, 98.6: 37 }
Unique features:
= {info ' name ' : ' John Doe ' , ' Age ' : 28, ' Gender ' : ' M ' , ' Hoppy ' : ' basketball ' }
keys: Gets a dictionary of all the key
Print (info.keys ()) # Get the dictionary all bonds dict_keys ([ 'name', ' age', 'gender', 'hoppy'])
for item in info.keys():
print(item)
values: Get all the values in the dictionary
Print (info.values ()) # Get all the values dict_values dictionary ([ 'Joe Smith', 28, 'M', 'basketball'])
for item in info.values():
print(item)
items: Gets a dictionary of all the key-value pairs
Print (info.items ()) # Get all key-value pairs in the dictionary dict_items ([( 'name', ' John Doe'), ( 'age', 28), ( 'gender', ' M'), ( 'hoppy', 'basketball')])
v1 for, v2 in info.items (): # key will be assigned to v1 v2
print(v1,v2)
get: to determine whether there is a dictionary key, without the creation
information = [ ' k1 ' : ' v1 ' , ' k2 ' : ' v2 ' ] v1 = info [ ' K3 ' ] V2 = info. GET ( ' K3 ' ) in Python is empty #none v3 = info.get('k3',666) print() #None data type that represents an empty (no functional, specifically for providing null)
pop / del: Delete
the info = { ' k1 ' : ' v1 ' , ' k2 ' : ' v2 ' } result = info.pop('k2') print(info,result) del info [ ' k1 ' ] print (info)
update: do not exist, add; there is, then update
the info = { ' k1 ' : ' v1 ' , ' k2 ' : ' v2 ' } # Does not exist, add; exist, updating info.update ({ ' K3 ' : ' V3 ' , ' K4 ' : ' V4 ' , ' K2 ' : 666 }) Print (info)
Exercises:
dict_ = {'name':'alex','password':'oldboy'} User = INPUT ( ' User: ' ) pwd = INPUT ( ' password: ' ) if user == dict_['name'] and pwd ==dict_['password']: print('ok') else: print('No')
2. collection