The dictionary is the only built-in mapping type in Python. The values in the dictionary are not in a special order, but are stored in a specific key.
A key can be a number, a string, or even a tuple.
1. Creating and Using dictionaries
Dictionaries can be created in the following ways:
Copy CodeThe code is as follows:
Phonebook = {' Alice ': ' 2341 ', ' Beth ':
Basic Python tutorial for. Net programmers-[Third Day] For dictionary usage and basic python tutorial
Today I learned how to use the dictionary. The so-called dictionary is actually Key-value pair data. A
the parameter is None) Dict.has_key (key) if the key (key) is in the word The code exists, returns TRUE, otherwise returns false. Introducing in and not on the Python2.2 versioninch, this method is almost obsolete, but still provides a working interface. Dict.items () Returns a list that contains a dictionary (key, value) to a tuple Dict.keys () returns a list containing the keys in the
world's publications so far.The dictionary here is not for catching up. Instead, remind crossing to think about how we use the dictionary: Check the index first (whether it's pinyin or the radicals), and then find the content by index.
This method can quickly find the target.
In Python, there is also a data similar to this, not only close, the name of this data
Python Day 1-getting started with common operations on the Python (4) dictionary, python Day 1
1 # dic = {[123, 3]: '000000'} # variable types cannot be used as Dictionary keys. value can be of any type 2 # dic = {(, 4 ): '000000'
the default value of the parameter is None)
Dict.has_key (Key)
Returns True if the key (key) exists in the dictionary, otherwise false is returned. After the Python2.2 version introduced in and not, this method is almost obsolete, but still provides a working interface.
Dict.items ()
Returns a list that contains a tuple (key, value
The dictionary in Python is the data structure for a key-value mapping in Python, and here's how to manipulate the dictionary gracefully.1.1 Creating a dictionaryThere are two ways python can create dictionaries, the first is usin
data type, such as a string, a number, or a tuple.
To access the value of a dictionary:
To access the dictionary element, you can use the familiar square brackets together to get the key to its value. The following is a simple example:
?
1 2 3 4 5 6
#!/u
Python mapping--Introduction to dictionary DictionariesDictionaries, as a kind of Python container, do not need to be ordered, so dictionaries are not part of the sequence. In this dictionary there are key (keys) and values (value). The
. viewvalues (
D. eq (d. iter (d. repr (d. fromkeys (d. pop (
D. format (d. le (d. setattr (d. get (d. popitem (
>>> D.
Add dictionary elements
>>> Nameinfo = {}
>>> Nameinfo ['A1'] = 'yangrong '# if the dictionary contains an a1 primary key, it overwrites the original value. If no, it is added.
>>> Nameinfo
{'A1': 'ya
Adding and traversing Python dictionary key-value pairsTo add a key-value pairFirst define an empty dictionarydic={}1Assign a value directly to a key that does not exist in the dictionary to adddic[' name ']= ' Zhangsan 'Dic{' nam
dictionary contains the following built-in functions:CMP (Dict1, DICT2) #比较两个字典元素. Len (dict) #计算字典元素个数, which is the total number of keys. STR (dict) #输出字典可打印的字符串表示. Type (variable) #返回输入的变量类型 returns the dictionary type if the variable is a dictionary. The Python
a list. Iterkeys, however, returns an iterator for the key.Pop pops up the value of the given key. Popitem pops up the last element (actually a random item), but if you want to remove and process the dictionary one after another, the popitem is much more efficient because you don't have to get a list of the dictionary's key values first.
Values and Itervalues m
Basic python tutorials-A Dictionary of notes
Create dictionary
A dictionary consists of multiple key-value pairs. Each key and corresponding value are separated by a colon, and items are separated by a comma. The entire
Dictionary dict:A dictionary is actually the equivalent of a map in Java to store key-value pairs. The data stored in it is unordered.If there is such a data:t1=[' name ', ' age ', ' sex 't2=[' Tom ', ' Male 'by Zip (t1,t2) you can get to such data [(' Name ', ' Tom '), (' Age ', ' + '), (' Sex ', ' male ')]The
First, what is a dictionaryA dictionary is the only type of mapping in the Python language.A hash value (key, key) and a pointer to an object (values, value) are a one-to-many relationship that is often considered a mutable hash table in the mapping type object.The Dictionary
DescriptionIt is obvious that the learning process of a dictionary in Python is the same as a list, mainly focusing on the following functions:>>> xpleaf.xpleaf.clear (Xpleaf.copy (Xpleaf.get (Xpleaf.has_key (Xpleaf.items (Xpleaf.keys (Xpleaf.pop ( Xpleaf.popitem (Xpleaf.setdefault (Xpleaf.update (
1. Basic operation--Create a dictionary>>> xpleaf = {...' N
This article mainly introduces the usage of the Dictionary (Dictionary) in python and provides detailed analysis of common operations such as creating, adding, and deleting a Python Dictionary in the form of an instance, for more information about the
List, String, Dictionary common operations in python, python string
The list operation is as follows:
A = ["haha", "xixi", "baba"]Increment: a. append [gg]A. insert [1, gg] To the place marked as 1, add ggDelete: a. remove (haha) deletes the first matched haha from left to right in the list.Del a. [0] Delete the value
Python dictionary operations
# Dictionary type# Create a dictionary # empty dictionary var = dict () print (var, type (var) var ={} print (var, type (var )) # create a dictionary with multiple data # 1.var = {'black': 'black', 'wh
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.