The code is as follows
Copy Code
#!/usr/bin/env python#-*-Encoding:utf-8-*-Dict2 = {}A = ' a 'Dict2.setdefault (a,1) #加入一个键和值 a,1A = ' B 'Dict2.setdefault (a,1) #加入一个键和值 b,1A = ' a 'Dict2.setdefault (a,2) #加入一个键和值 a,1, the resulting duplicate key cannot be repeated by adding {' A ': 1, ' B ': 1}Print Dict2#遍历key和valueFor key in Dict2.keys ():print ' key ', key, ' value ', Dict2[key]Print Dic
prototypes: Def index (self, value, Start=none, Stop=none)Usage:
Tuple1.index (value,m,n) Find value in #在 [m,n) interval
Some functions defined within the dictionary Dict
Clear: Clears the contents of the dictionaryFunction prototype: def clear (self)Usage:
Dict1.clear ()
elements with a colon, "key" before the colon, and "value" after the colon.>>> a={' breakfast ': ' Milk eggs ', ' dinner ': ' Fruit salad ', ' Lunch ': ' Cola Steak '}
#python允许不同的键拥有相同的值, so the following syntax is correctb={"one": "Qing", "two": "Qing"}
#有两种方法可以读取字典中的值, use the dictionary [key] to read the value d
This article describes how to use the dictionary in python. For more information, see the dictionary.
Dictionary
1. Set of key-value pairs (map)
2. The dictionary is a set of data surrounded by braces "{}".
3. The
elements of the list x to build a dictionary, the key value is the list of each element, value is None4. Get (key) to find value based on key5. Hash_key (x) find if the dictionary contains a key
A dictionary is the only type of mapping in the Python language. A hash value (key, key) and a pointing object (value, value) in a mapped type object are a one-to-many relationship that is often considered a mutable hash table. The object of the
The Python dictionary contains the following built-in methods:
Serial Number
Functions and descriptions
1
Radiansdict.clear ()Delete all elements in a dictionary
2
Radiansdict.copy ()Returns a shallow copy of a dictionary
3
Radiansdict.fromkeys
This article analyzes the method of sorting Python dictionaries. Share to everyone for your reference. Specifically as follows:
1, the preparation of knowledge:
In Python, the dictionary dictionary is a built-in data type, an unordered storage structure, and each element is a Key-
:
Copy Code code as follows:
>>> x=1
>>> y= (x=x+1)
Syntaxerror:invalid syntaxWith #! The beginning is called the organization line, which tells you which interpreter the Linux/unix system should run when you execute your program. For example: #!/usr/bin/python
The beginning of a # is called a comment line.
Second, conditional statements
Control Flow statement: Any conditional expression can be established by using the Or,
, ' Y ': 2, ' Z ': 3, ' W ': 666}, Dict1 also returns {' X ': 1, ' Y ': 2, ' Z ': 3, ' W ': 666}But modifying dict2 does not affect Dict1, such as: dict2[' V '] = 88, then Dict2 returns {' X ': 1, ' Y ': 2, ' Z ': 3, ' V ': 88}, while Dict1 still returns {' X ': 1, ' Y ': 2, ' Z ': 3, ' W ': 666}Dict1.pop (' x ') removes the ' X ' key and the corresponding value from the dictionary and returns the valueDel
The derivation of comprehensions (also known as analytic) is a unique feature of Python. Derivation is the structure of another new data series that can be built from one data series.
List-derived
Dictionary (dict) derivation formula
Set (set) derivation
List derivation one, use [] to generate listBasic format for inch if the condition]expression# list generates an element expr
Describes the concepts and usage of ing type (dictionary) Operators in Python, and python operators.
Ing Type Operator
(1) standard operators
The dictionary can work with all standard type operators, but it does not support operations such as concatenation and repetition. These operations are meaningful to the sequence
dictionary:In [19]: dict1.values()Out[19]: [1, 123, ‘hello‘]Use the keys value to find values:In [16]: dict1Out[16]: {1: 123, ‘a‘: 1, (‘qq‘, ‘wx‘): ‘hello‘}In [22]: dict1.get(1)Out[22]: 123If the keys value is not in the dictionary being taken, the value is not returned, bu
. TuplesLists are similar to tuples, lists are mutable, tuples are immutable# # String Conversion Narimoto Group:str1 = ' ABCDEFGHIGKLMN 'Print (tuple (STR1))(' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' g ', ' k ', ' l ', ' m ', ' n ')# # Tuple AA = (' A ', ' B ', ' C ', ' abc ', ' Hello ')When you have a single tuple, be careful to add commas :b = (' mn ',)Print (type (b))Otherwise, the Python parser is not recognized as a tuple
Basic composition and usage of the Python dictionary
#! /Usr/bin/env python#-*-Coding: UTF-8 -*-"""Old Rules: Environment 2.7.x in the following method. Please remember the print format (the output content should be placed in parentheses) for users of version 3.x or later)Basic composition and usage of dictionariesDict = {key:
; variable name [key name]ValueAdd toDictionaries are added in different ways: Dictionary variable name [newly added key name] = value corresponding to new keyModifyDictionary modification format: dictionary variable name [to modify the value corresponding to the key name] = new valueDeleteThere are three common ways t
Python dictionary FunctionsMethod Name operation dict. cleara () deletes all the elements in the dictionary. copya () returns a copy of the Dictionary (Shortest copy) dict. fromkeysc (seq, val = None) c creates and returns a new dictionary, which uses the elements in seq as
DictPython 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.For example, suppose you want to find the corresponding score according to the name of the classmate, if you use list, you need two lists:name
Students who have studied Java know that there is a data format for key-value pairs in Java--mapIn Python, there is a format similar to map, which is the dictionary (Dict)1. Create a Dictionary type Objectuser={ "username":"Dylan", " Age ":", "password":"123" }2. TraverseUser={ "username":"Dylan", " Ag
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.