string representation.
4. type (variable): return the input variable type. if the variable is a dictionary, the dictionary type is returned.
The Python dictionary contains the following built-in methods:1. radiansdict. clear (): deletes all elements in the dictionary.2. ra
1, the basic situation of the dictionary is key:value, such a key-value pair mode, and the dictionary can store the amount of information is almost unrestricted. The corresponding value is obtained by accessing the key. Use between keys and values (:) separate. The dictionary
The dictionary is the only built-in mapping type in Python. The values in the dictionary are not in a particular order, but are stored in a particular 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 Code code as follows:
Phonebook = {' Alice ': ' 2341
Data structures that refer to values by name in Python are called mappings (mapping). Dictionaries are the only types of mappings in Python that are built in (the Python interpreter itself supports, and does not require import). The values in the dictionary are not in a special order and are stored under a specific key
refers to the constant . That point ' a ', it cannot be changed to point to ' B ', pointing to a list, cannot be changed to point to other objects, but the list itself is variable! To create a tuple that does not change content, you must ensure that each element of the tuple itself cannot be changed. List and tuple are Python's built-in ordered set, one variable, one immutable. Choose to use them as needed.The two commonly used methods of a tuple are: count () counts the number of elements of a
4
radiansdict . Get (Key, Default=none) Returns the value of the specified key if the value does not return the default value in the dictionary
5
radiansdict.has_key (key) if the key Returns true in
, ' C ': None, ' e ': None, ' d ': none, ' F ': none}{' y ': 2, ' x ': 1}{' Key2 ': 2, ' Key1 ': 1}Zip ([iterable, ...])Zip () is an intrinsic function of Python that takes a series of iterated objects as parameters, packages the corresponding elements in the object into tuple (tuples), and then returns a list of these tuples. If the length of the passed parameter is not equal, the length of the returned list is the same as the object with the shortes
The dictionary is the only type of mapping in python, its hash value (key Key) and the point to object (value) is a one-to-many relationship;A dictionary is a mutable object, is a container type, can store any number, any type of mapping type, and is enclosed in curly braces
Python dictionary type usage example
DictionaryIt is the only ing type in python. It is represented by braces {}. A dictionary entry is a key-value pair. The method keys () returns the dictionary Key List, values () returns the li
position. The default value is the last object 12. list. remove (obj): Delete the obj object from the list.
Tuples
It can contain different types of objects, but it is immutable. You cannot add or remove elements. You can use () to define eg: aTuple = (123, 'abc', 4.56, ['inner ', 'LIST'], 7-9j)
1. tuple (obj): Convert object obj to tuple object. obj can be any string or list 2. the del, cmp, len, max, and min methods for the list
= {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4}
d2 = {' B ': 2, ' C ': 3, ' d ': 3, ' E ': 5}
To find the items in these two dictionaries that have a common key, that is to get the result {' B ': 2, ' C ': 3}
Solution
We know that generally through the d.items() method to traverse the dictionary, the d.items() method returned object is a class collection object, support the basic operation of the set, s
电话号码还是地址?Request=raw_input (' Phone number (p) or address (a)? ')#使用正确的键Key=request #如果请求的既不是 ' P ' is also not ' a 'If request = = ' P ': key= ' phone 'If request = = ' A ': key= ' addr 'Default value provided by the #使用get ()Person=people.get (name,{})Label=labels.get (Key,key)Result=person.get (Key, ' not available ')print '%s '%s is%s. "% ( Name,label,result)----------------------------------------Name:
publishing.The dictionary here is not for catching up. Instead, remind reader to think about how we use the dictionary: First look at the index (whether it is pinyin or the character), and then find the appropriate content through the index.
This method can quickly find the target.
In Python, there is also a kind of data similar to this, not only close, the na
. () [(0 1 ( 0, 2 (0, 3 /span> however, in Python3 I get something like>>> zip(list(newdict.keys()), list(newdict.values()))zip object at 0x7f367c7df488>Okay, sorry, I just figured out, which you had to use a list() function around zip() too. list ( Zip (newdict. Values (), Newdict. ()) [(0 1 ( 0, 2 (0, 3 /span> This is really something one have to get
', ' Python Web Site ')]
Removed from
Pop ()1. Get the value of the given key and remove the key-value pair from the dictionary
D = {' X ': 1, ' Y ': 2|D.pop (' x ')D
1{' Y ': 2}
Popitem ()1. Remove random items
d = {' URL ':
The dictionary in Python is the data structure of a key-value mapping in Python, and here's how to manipulate the dictionary gracefully.1.1 Creating a dictionary
Python has two ways to
This article mainly introduces the Python dict dictionary structure operation method learning notebook. The dictionary operation is the basic knowledge in the Python getting started learning. For more information, see
I. Basic dictionary Methods
1. Create a
operatorsAs with strings, you can use the + and * numbers to perform operations between tuples. This means that they can be combined and copied, and a new tuple is generated after the operation.7.5, tuple index InterceptL = (' spam ', ' spam ', ' spam! '); Print l[2]; # ' spam! ' Print L[-2]; # ' Spam ' Print l[1:]; #[' Spam ', ' spam! ']7.6, tuple built-in functionsCMP (Tuple1, tuple2) compares two elements of tuples. Len (tuple) calculates the number of tuple elements. Max (tuple) returns th
does not exist, the second parameter value is returned. We can see that get is more elegant.1.4 update/Add
The Python dictionary can use keys as indexes to access, update, and add values.
>>> info = dict()>>> info['name'] = 'cold'>>> info['blog'] = 'linuxzen.com'>>> info{'blog': 'linuxzen.com', 'name': 'cold'}>>> info
Search and delete of dictionary dictionaryHow dictionaries are created:# Create Dictionary Type info = { ' name ': ' Li Bai ', ' age ': ' + ' , ' sex ': ' male '}msg = { ' User01 ': ' Longzeluola ', ' user02 ': ' Xiaozemaliya ', ' user03 ': ' Cangjingkong '}When a dictionary stores data, it takes the form of Key:value, which is a set of unordere
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.