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/02 Report--
This article mainly explains "what is the use of Python data type dictionary". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the use of Python data type dictionary".
The unordered nature and creation of dictionaries:
Lists are ordered combinations of objects, and dictionaries are unordered collections of objects.
The elements in the dictionary are accessed by keys, each key-value (key= > value) pair is separated by a colon (:), each pair is separated by a comma (,), and the entire dictionary is included in curly braces ({}) in the following format:
D = {key1: value1, key2: value2}
The key must be unique, but the value is not necessary.
Values can take any data type, but keys must be immutable, such as strings, numbers, or tuples.
Dictionary example:
Dict = {'Alice':' 2341, 'Beth':' 9102, 'Cecil':' 3258'} can also create a dictionary in this way: dict1 = {'abc': 456} dict2 = {' abc': 123,98.6: 37} dict3 = {} access the values in the dictionary:
Put the corresponding keys in square brackets
Example:
#! / usr/bin/pythondict = {'Name':' Zara', 'Age': 7,' Class': 'First'} print "dict [' Name']:", dict ['Name'] print "dict [' Age']:", dict ['Age']
Output: dict ['Name']: Zaradict [' Age']: 7
There are two ways to traverse the dictionary:
The first kind: for kpene v in dict.items (): print (kmenv). The second kind: efficient for key in dict: print (key,dict [key]) modifies the dictionary:
The way to add new content to the dictionary is to add new key / value pairs and modify or delete existing key / value pairs.
Example:
#! / usr/bin/pythondict = {'Name':' Zara', 'Age': 7,' Class': 'First'} dict [' Age'] = 8 # update existing entrydict ['School'] = "DPS School" # Add new entryprint "dict [' Age']:", dict ['Age'] print "dict [' School']:", dict ['School'] output result: dict [' Age']: 8dict ['School']: DPS School delete dictionary element:
Can delete a single element can also empty the dictionary, emptying only one operation.
Example:
#! / usr/bin/python#-*-coding: UTF-8-*-dict = {'Name':' Zara', 'Age': 7,' Class': 'First'} del dict [' Name'] # Delete key is' Name' entry dict.clear () # clear all entries in del dict # delete dictionary print "dict ['Age']:", dict [' Age'] print "dict ['School']:" Features of dict ['School'] 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. 2) the key must be immutable, so it can be served as a number, string, or tuple, so it is not possible to use a list as a dictionary built-in function & method:
Python dictionary built-in function:
| sequence number | function and description | | 1 | cmp (dict1, dict2) compare two dictionary elements. | | 2 | len (dict) counts the number of dictionary elements, that is, the total number of keys. | | 3 | printable string representation of str (dict) output dictionary. | | 4 | type (variable) returns the input variable type, and returns the dictionary type if the variable is a dictionary. | |
The built-in method of Python dictionary:
| | sequence number | function and description | | 1 | radiansdict.clear () deletes all elements in the dictionary | | 2 | radiansdict.copy () returns a shallow copy of a dictionary | | 3 | radiansdict.fromkeys () creates a new dictionary, using the elements in the sequence seq as the keys of the dictionary, and val is the initial value of all keys in the dictionary | | 4 | radiansdict.get (key, default=None) returns the value of the specified key | | | 5 | radiansdict.has_key (key) if the key returns true in the dictionary, otherwise return false | | 6 | radiansdict.items () returns a traverable (key, value) tuple in the list | | 7 | radiansdict.keys () returns all the keys in a dictionary in a list | | 8 | radiansdict.setdefault (key, default=None) is similar to get (), but if the key does not exist in the dictionary | 8 | The key will be added and the value will be set to default | | 9 | radiansdict.update (dict2) updates the key / value pair of the dictionary to dict | | 10 | radiansdict.values () returns all the values in the dictionary in a list | Dictionary variables are unpacked > d3LING D4 = {'xdye 32heroic dictionary 80}} > print (d3) y > print (d4) x Thank you for your reading. These are the contents of "how to use Python data Type Dictionary" after the study of this article. I believe that you have a deeper understanding of the use of Python data type 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.