Dictionary: an object associated with an array or a hash list that can be indexed by a keyword. Usage: defines an object that can contain multiple named fields, it can also be used as a container dictionary to quickly search for unordered data. it is the most comprehensive data type in python. it is most commonly used in programs to store and process data dictionaries: an associated array or a scattered list, objects that can be indexed by keywords.
Usage of the dictionary: defines an object that can contain multiple naming fields, and can also be used as a container to quickly search for unordered data
The dictionary is the most complete data type in python. it is most commonly used in the program to store and process data.
How to create:
1. enter a value in {} to create an empty Dictionary;
2. use dict () to create an empty Dictionary
Data = {"name": "shenzhang Taibao Dai Zong", 'Title': 'Day start', 'age': 45, 'price': 490}
To access the dictionary members, use the keyword index operator s [name]:
name = data['name']; title = data['title']; age = data['age']; print(name); print(title); print(age);
Output result:
Shenzhang Taibao Dai zongtian Speedstar 45
How to insert or modify an object:
Data ['book'] = 'alianshan 108 when the Water Margin is passed '; # insert data ['name'] = 'Shark's fin, tiger, and Thunder '; # modify data ['title'] = 'day-out start ';
Output result:
The Water Margin of Liangshan 108 will insert Wings, Tigers, thunder, and stars
String is a common keyword type
Search for unordered data:
prices = { 'apple' :3.4, 'banana' : 4, 'orange' : 2.5, 'lemon' : 3.7, 'pear' : 1.8 }
applePrice = prices['apple'];
Output result:
3.4
How to determine whether an item is a member of the current dictionary:
1. use the in operator to test whether a content item is a dictionary member.
if "grape" in prices: p = prices['grape']; else: p= 0; print(p);
Output result:
0
2. use the system method get to determine whether it is a dictionary member.
p = prices.get('grape',0); print(p);
Output result:
0
To obtain a list of dictionary keywords, you only need to convert the dictionary to a list:
pricelist = list(prices);
Output result:
['orange', 'lemon', 'pear', 'banana', 'apple']
Delete the dictionary element method del:
del prices['pear'];
Output result:
{'apple': 3.4, 'banana': 4, 'lemon': 3.7, 'orange': 2.5}
Summary:
1. what is the dict dictionary? : It is a correlated array or a hash.
2. create a dictionary: 1, {} 2, dict ()
2. usage of the dictionary: used to quickly search for unordered data. it is often used to store and process data.
3. use the dictionary keyword index to retrieve data
4. dictionary insertion and modification: use the keyword index to add or modify the format s [name] = 'data ';
5. determine whether the element exists in the dictionary: 1, in 2, get
6. how to obtain dictionary Keywords: The list is declared as a list
6. delete the element in the dictionary: del method
The above is the dictionary content in python. For more information, see The PHP Chinese website (www.php1.cn )!