In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Python Dictionary". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's idea to study and learn how to use Python Dictionary.
Dictionaries are another variable container model and can store objects of any type.
Each key value key= > value of the dictionary is divided by a colon: split, each key value pair is divided by a comma, and the whole dictionary is included in curly braces {}
The format is as follows:
D = {key1: value1, key2: value2}
Note: as the keyword and built-in function of Python, dict is not recommended to be named dict.
The key is generally unique, if you repeat the last key-value pair will replace the previous one, the value does not need to be unique.
> tinydict = {'averse: 1,' baked: 2, 'baked:' 3'} > tinydict ['b']'3' > tinydict {'axed: 1,' baked:'3'}
Values can take any data type, but keys must be immutable, such as strings, numbers, or tuples.
A simple dictionary example:
Tinydict = {'Alice':' 2341, 'Beth':' 9102, 'Cecil':' 3258}
You can also create a dictionary like this:
Tinydict1 = {'abc': 456} tinydict2 = {' abc': 123,98.6: 37} 1. Access the values in the dictionary
Put the corresponding key in the familiar square brackets, as an example:
Example:
Tinydict = {'Name':' Zara', 'Age': 7,' Class': 'First'} print "tinydict [' Name']:", tinydict ['Name'] print "tinydict [' Age']:", tinydict ['Age']
The output result of the above example:
Tinydict ['Name']: Zara
Tinydict ['Age']: 7
If you access the data with keys that are not in the dictionary, the output error is as follows:
Example:
Tinydict = {'Name':' Runoob', 'Age': 7,' Class': 'First'} print "tinydict [' Alice']:", tinydict ['Alice']
The output result of the above example:
Tinydict ['Alice']: Traceback (most recent call last): File "test.py", line 5, in print "tinydict [' Alice']:", tinydict ['Alice'] KeyError:' Alice'2.
The way to add new content to the dictionary is to add new key / value pairs and modify or delete the following examples of existing key / value pairs:
Example:
Tinydict = {'Name':' Zara', 'Age': 7,' Class': 'First'} tinydict [' Age'] = 8 # Update tinydict ['School'] = "RUNOOB" # add print "tinydict [' Age']:", tinydict ['Age'] print "tinydict [' School']:", tinydict ['School']
The output result of the above example:
Tinydict ['Age']: 8tinydict [' School']: RUNOOB3. Delete dictionary element
Can delete a single element can also empty the dictionary, emptying only one operation.
Show delete a dictionary with the del command, as an example:
Example:
Tinydict = {'Name':' Zara', 'Age': 7,' Class': 'First'} del tinydict [' Name'] # delete key is' Name' entry tinydict.clear () # clear dictionary all entries del tinydict # delete dictionary print "tinydict ['Age']:", tinydict [' Age'] print "tinydict ['School']:", tinydict [' School']
But this throws an exception because the dictionary no longer exists after using del:
Tinydict ['Age']: Traceback (most recent call last): File "test.py", line 10, in print "tinydict [' Age']:", tinydict ['Age'] NameError: name' tinydict' is not defined
Note: the del () method is also discussed later.
3.1 characteristics of dictionary keys
Dictionary values can take any python object without restriction, either standard or user-defined, but not keys.
There are two important points to remember:
1) the same key is not allowed to appear twice. If the same key is assigned twice during creation, the latter value will be remembered
The following is an example:
Tinydict = {'Name':' Runoob', 'Age': 7,' Name': 'Manni'} print "tinydict [' Name']:", tinydict ['Name']
The output result of the above example:
Tinydict ['Name']: Manni
2) the key must be immutable, so it can be served as a number, string, or tuple, so a list is not allowed.
The following is an example:
Tinydict = {['Name']:' Zara', 'Age': 7} print "tinydict [' Name']:", tinydict ['Name']
The output result of the above example:
Traceback (most recent call last):
File "test.py", line 3, in
Tinydict = {['Name']:' Zara', 'Age': 7}
TypeError: unhashable type: 'list'
4. Dictionary built-in function & method
The Python dictionary contains the following built-in functions:
The ordinal function and the description 1cmp (dict1, dict2) compare two dictionary elements. 2len (dict) counts the number of dictionary elements, that is, the total number of keys. 3str (dict) outputs a printable string representation of the dictionary. 4type (variable) returns the input variable type, or the dictionary type if the variable is a dictionary.
The Python dictionary contains the following built-in methods:
The ordinal function and description 1dict.clear () Delete all elements in the dictionary 2dict.copy () returns a dictionary shallow copy 3dict.fromkeys (seq [, val]) to create a new dictionary, using the elements in the sequence seq as the dictionary keys, and val returns the value of the specified key for the initial value 4dict.get (key, default=None) corresponding to all the keys in the dictionary, if the value does not return the default value 5dict.has _ key (key) if the key returns true in the dictionary dict Otherwise, return false6dict.items () to return a traverable (key, value) tuple array 7dict.keys () in a list to return a dictionary-owned key 8dict.setdefault (key, default=None) is similar to get (), but if the key does not exist in the dictionary The key will be added and the value will be set to default9dict.update (dict2) the key / value pair of the dictionary dict2 will be updated to 10dict.values () in dict to return all values in the dictionary 11pop (key [, value]) to delete the value corresponding to the dictionary given key key, and the return value will be the deleted value. The key value must be given. Otherwise, the default value is returned. 12popitem () returns and deletes the last pair of keys and values in the dictionary. Thank you for reading, the above is the content of "how to use Python Dictionary". After the study of this article, I believe you have a deeper understanding of how to use Python Dictionary, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.