The list is a class in Python. A specific table, such as nl [1, 3, 8], is an object of this class. We can call some methods of this object, such as nlappend (15 ). We will introduce a new class, dictionary ). Similar to the list, the basic tutorial introduces basic concepts, especially objects and classes.
The advanced tutorial further expands the basic tutorial
relational data.The dictionary is unordered before Python 3.5 contains 3.5, but is ordered after 3.6.2, the dictionary additions and deletions and other operationsThe key is unique when the same key is present in the back of the overlay front. For example:DIC = { ' name ': ' Alex ', ' name ': ' Wusir '}print (DIC)The key must be a data type that is
object encode() .Back to top of pageChanges in the formatting of stringsMany Python programmers feel that the built-in operator for formatting strings % is too limited, because:
It is a binary operator and can accept up to two parameters.
In addition to formatting string parameters, all other parameters must be squeezed with a tuple (tuple) or a dictionary (
3, Del d[' a '] #删除键值为 ' a ' element
Traversing elements
For K in D:
print ' d[%s]= '% k,d[k]
Or
For k,v in D.items ():
print ' d[%s]= '% k,v
Or
For k,v in D.iteritems ():
print ' d[%s]= '% k,v
Or
For k,v in D.viewitems ():
print ' d[%s]= '% k,v
Items (), Iteritems () and viewitems () differences
Python2.x's items () is a list that contains all the elements of dict, like the one above, but because of this too much memory, it was later added (note:
', '), (' Spatzleblock '), ( ' Snitzelhogen ', ]) inventory{' foozelator ': 123, ' frombicator ': +, ' snitzelhogen ': +, ' Spatzleblock ': 34} popmethod to delete an elementpopMethod can be used to pop up values for a key in a dictionary, and you can specify default parameters:`d.pop(key, default = None)`Deletes and returns the value corresponding to the key in the dictionary key , and returns th
present in the dictionary, it is assigned a value by Dict[key]=default.
Dict.setdefault (Key,default=none)
Similar to the method set (), if the key is not present in the dictionary, it is assigned a value by Dict[key]=default.
Practice:
Shopping Cart Program requirements:
1. After starting the program, let the user enter the salary, and then print the list of goods;
2. Allow
The bidict module uses a one-to-one ing structure to bring a two-way dictionary to Pyhton. it can make better use of the Python slicing function. here we will learn how to use the bi-way dictionary structure of the bidict module in Python:
Quick start
The module provides three classes to handle one-to-one ING operatio
and key is not in the dictionary, a KeyError is raised. d. popitem () Remove and return an arbitrary (key, value) pair fro M the dictionary. d. setdefault (key [, default]) If key is in the dictionary, return its value. if not, insert key with a value of default and return default. default ults to None. d. update (B) Update the
Python converts string and dictionary types.
This document describes how to convert string and dictionary types in Python by using examples. The specific method is as follows:
1. convert a dictionary (dict) to a string (string)
We can easily convert the
Python has three built-in data structures: list, ancestor, and Dictionary. This article introduces these three data structures and provides examples, let's take a closer look at the differences between the three.
1. list)
List is the data structure that processes a group of ordered projects. you can store a series project in a list.
The items in the list should be included in square brackets so that
1. Dictionary creationThe dictionary is separated by a colon between each key and its value, and the items are separated by commas, and the entire dictionary is surrounded by a pair of curly braces. An empty dictionary does not include any item {}.2. Dict functionLike LIST,TUPLE,STR, you can use the Dict function to bu
Dict Dictionary TypesDict Dictionary type provides methods: (Total 9 kinds)1. Clear the DictionaryDic.clear ()2, copy the dictionary shallow copyDic.copy ()3. Create a dictionary based on the sequence and specify a uniform value (none by default); This method is a static met
)False: Roman numerals, Chinese numeralsError:byte number (single byte)32, S.isnumeric (), BOOLWhether the string contains only numbersTrue:unicode numbers, full-width numerals (double-byte), Roman numerals, Chinese numeralsFalse: NoneError:byte number (single byte)33, S.isdigit (), BOOLWhether the string contains only numbersTrue:unicode number, byte digit (single byte), full-width digit (double byte), Roman numeralsFalse: Chinese numeralsError: None34, S.isspace (), BOOLWhether the string cont
form a list >>> dict = {' X ': 1, ' Y ':2}>>> dict.items () dict_items ([' X ', ' 1 '), (' Y ', 2)]) variable unpacking (both sides to be consistent) >>> Dict = {' x ': 1, ' Y ':2}> >> t1,t2 = Dict.items () >>> print (T1) (' x ', 1) >>> print (T2) (' Y ', 2) >>gt ; Dict.keys (return key list) >>> Dict = {' x ': 1, ' Y ': 2, ' Z ':3}>>> Dict.keys () Dict_keys ([' X ', ' y ', ' z ']) dict.value ( return value list) >>> dict = {' x ': 1, ' Y ': 2, ' Z '
The following small series will bring you a python dictionary (dict) key and value sorting. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the following small series to bring you a python dictionary (dict) key and value sorting. I think this is quite good. now I
Dictionary: Dict1. The dictionary is the only type of mapping in Python and consists of value pairs enclosed in {};2, the dictionary key is unique, at the time of saving, according to key to calculate a memory address, how to put value in this memory, this is called the hash algorithm;
DictionaryPython built-in dictionary: dict support, Dict full name dictionary, in other languages also known as map, using key-value (Key-value) storage, with a very fast search speed.If implemented with Dict, only need a "name"-"score" of the table, directly based on the name of the results, no matter how large the table, the search speed will not be slow. Write a dict in
There are many simple functional applications in the Python programming language, but they are very powerful in actual programming. For example, the application of the Python dictionary is one of the most important applications. Next, we will give a detailed introduction to the Python
if the index value is not specified. This method takes out the corresponding element and then deletes the element from the list >>> print (A.pop ()) 9 >>> Print (a) [3, 4, 5, 6, 7, 8] nbsp; >>> Print (a.pop (0)) 3 >>> print (a) [4, 5, 6, 7, 8]
#使用set () can delete duplicate values in the list>>> e=["A", "a", "B", "C", "B"]>>> Print (e)[' A ', ' a ', ' B ', ' C ', ' B ']
>>> E=set (E)>>
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.