Dictionary of Python sequences
A dictionary in Python is equivalent to a Map object in Java
Dictionary features:
- Read by key instead of index
- A dictionary is an unordered collection of arbitrary objects
- Dictionaries are variable and can be nested arbitrarily
- Key in dictionary must be unique
If the key appears twice, the latter value will be remembered - Key in dictionary must be immutable
Keys can use numbers, strings, tuples, not lists
Create and delete
dictionary = {'qq': '61553696', 'Blog': 'Hearing impaired old boy', 'Telephone': '8888888'} print(dictionary) """Two ways to create an empty dictionary""" dictionary = {} dictionary1 = dict() """Existing data creation dictionary""" # 1. Map function create dictionary name = ['Zhang San', 'Li Si', 'Wang Wu'] age = ['19', '18', '17'] # The zip() function combines elements of multiple lists or tuples into tuples, and returns the zip object of these contents dictionary = dict(zip(name, age)) print(dictionary) # 2. Key value pair create dictionary dictionary = dict(qq='61553696', Blog='Hearing impaired old boy', Telephone='8888888') print(dictionary) # Tuples and lists creating Dictionaries name_tuple = ('Zhang San', 'Li Si', 'Wang Wu') age = ['19', '18', '17'] dict1 = {name_tuple: age} print(dict1) # The result is: {('zhang San ',' Li Si ',' Wang Wu '): ['19', '18', '17']} # Clear all elements of the dictionary, the source dictionary becomes empty dict1.clear() print(dict1) # {} # del command to delete the entire dictionary del dict1 print(dict1) # NameError: name 'dict1' is not defined # The pop() method deletes and returns the element of the specified 'key' # The popitem() method deletes and returns an element in the dictionary
Key value pair ACCESS Dictionary
# When getting the value of a specified key, an exception is thrown if the specified key does not exist dictionary = {'Zhang San': 18, 'Li Si': 19, 'Wang Wu': 20} # print(dictionary['Zhao Liu']) # KeyError: 'Zhao Liu' # Solution 1: use if statement print(dictionary['Zhao Liu'] if 'Zhao Liu' in dictionary else 'Dictionary does not have this person') # Solution 2: the key specified by get() method does not exist and returns a default value print(dictionary.get('Zhao Liu')) # None print(dictionary.get('Zhao Liu', 'Dictionary does not have this person'))
Ergodic dictionary
# The items() method gets the 'key value pair' tuple list dictionary = {'Zhang San': 18, 'Li Si': 19, 'Wang Wu': 20} for item in dictionary.items(): print(item) """ //Output results: ('Zhang San', 18) ('Li Si', 19) ('Wang Wu', 20) """ # Get each key and value for key,value in dictionary.items(): print(key, value) """ //Output results: //Zhang San 18 //Li Si 19 //Wang Wu 20 """ # values() returns a list of 'values' for the dictionary print(dictionary.values()) # dict_values([18, 19, 20]) for value in dictionary.values(): print(value) # keys() returns the dictionary's list of 'keys' print(dictionary.keys()) # dict_keys(['zhang San ','li Si','wang Wu ']) for key in dictionary.keys(): print(key)
Add, modify, and delete dictionary elements
# Add to dictionary = dict((('Zhang San', 18), ('Li Si', 20), ('Wang Wu', 33))) dictionary['Zhao Liu'] = 29 print(dictionary) # {'Zhang San': 18, 'Li Si': 20, 'Wang Wu': 33, 'Zhao Liu': 29} # modify dictionary['Li Si'] = 100 print(dictionary) # {'Zhang San': 18, 'Li Si': 100, 'Wang Wu': 33, 'Zhao Liu': 29} # Delete del del dictionary['Li Si'] print(dictionary) # {'Zhang San': 18, 'Wang Wu': 33, 'Zhao Liu': 29} # del dictionary['haha'] # Delete the exception thrown by the nonexistent key KeyError: 'haha' # Solve if 'haha' in dictionary: del dictionary['Ha-ha'] print(dictionary)
Dictionary derivation
- Generate a dictionary quickly, similar to list derivation
# Generate a dictionary of 4 random numbers, where the key of the dictionary is represented by a number import random randomdict = {i:random.randint(10, 100) for i in range(1, 5)} print('The generated dictionary is: ', randomdict) # List generation dictionary name = ['Zhang San', 'Li Si', 'Wang Wu'] age = [18, 19, 20] dictionary = {i: j for i, j in zip(name, age)} print(dictionary)```