The dictionary in Python belongs to an advanced data structure, which is based on the hash table, and the complexity of the hash table in finding an element is basically O (1), so it is a very useful data structure. The following is a brief introduction to the operation of some dictionaries of capitalCreate a dictionary, assign a value to a dictionarynamerank={}n
,python 3.0 does not have the functionprint ' Has_key ', D.has_key (' Mali ')#7 the. Items method returns all dictionary items as a list, each of which comes from (key, value)d={' Mali ': +, ' Shasha ': 85}Print D.items ()The #iteritems method works roughly the same, but returns an iterator object instead of a listIt=d.iteritems ()Print itPrint List (IT)#8 the. K
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
= {' name ': ' Tom ', ' Age ': '}dict1.pop ' print dict1 run result: {' name ': ' Tom '} 12,popitem randomly Delete Keydict1 = {' name ': ' Tom ', ' Age ': '}dict1.popitem ' () print Dict1 run result: {' name ': ' Tom '} 13,setdefault if the key is not in the dictionary, set the key and value, and if so, use the default and return the corresponding values. Dict1 = {' name ': ' Tom ', ' Age ': '}dict1.setdefault
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 corresponding to 0 as the subscriptA. NO cont
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{' name ': ' Zhangsan '}123If key or value are variables, you can also use this methodKey= ' age 'Value=30Dic[key]=valueDic{' Age ': +, ' nam
dictionary D2. d.copy () Shallow copy of Dictionary d, returns a new dictionary with the same key value pair as D3, d.get (x [, y]) returns the value corresponding to the key x in the dictionary d, the key x does not exist, returns y, and the default value of Y is none .4, d.items () returns all Key-value pairs in
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
that the brackets should be changed to curly braces. Direct illustrative examplesEg: uppercase and lowercase key mergingMcase = {'a': 10,'b': 34,'A': 7,'Z': 3}mcase_frequency={k.lower (): Mcase.get (K.lower (), 0)+Mcase.get (K.upper (), 0) forKinchMcase.keys ()ifK.lower ()inch['a','b']}Printmcase_frequency#Output Results{' A ': +, ' B ':Eg: Quick change of key and valueMcase = {'a'b': in Mcase.items ()}print mcase_frequency# Output Results {ten: ' A
] Define a collection to define an empty collection in [the]: S1 = set () in []: Type (S1) out[45]: Set dictionary can be converted to set in []: D = dict (a= 1, b=2, c=3) in []: set (d) out[47]: {' A ', ' B ', ' C '} set is an unordered data type; in []: s = {2, 3, N, 89}in []: S.add (+) in [[]: P Rint Sset ([2, 3, 1
the dictionaryPrint (K.values ())[' Ten ', ' abc ', ' Tiantian ']Print (K.itervalues ())# # Iteritems Traversal# # Items TraversalPrint (K.iteritems ())Print (K.items ())[(' Age ', ' ten '), (123, ' abc '), (' Name ', ' Tiantian ')]"Iteritems () better use of resources than items ()"For x, y in K.iteritems ():Print (x, y)Returns a single tuple:(' Age ', ' 10 ')(123, ' abc ')(' name ', ' Tiantian ')# # POP Delete dictionary specifies keys and his valu
This article describes in detail the usage of python dictionary operations. For more information, see
Basic syntax:
Dict = {'ob1': 'computer ', 'ob2': 'Mouse', 'ob3': 'printer '}
Tips:
The dictionary contains the list: dict = {'angrong ': ['23', 'It'], "xiaohei": ['22', 'Dota']}.
The dictionary contains the following d
dictionary2, D.copy (), DReturns a copy of a dictionary3, D.pop (K[,d]), valueThe value corresponding to the K key is ejected and the key value is removed. If the K key is not found but the value of D is set, the value of D is returned. If the D value is not set, then the K key is not found, then error.4, D.popitem (), (K, V)In the form of Ganso, a key value pops up (usually the first key value of a stack)5, D.keys (), a Set-like objectAll keys are returned in a similar list (actually returning
methods: Count and IndexTuple definition: names= (' Jim ', ' Lucy ')Third, the dictionaryA dictionary is a Key-value data type and is unordered.1. Dictionary-related operations#字典定义: info={' stu001 ': ' Lily ', ' stu002 ': ' Jack ', ' stu003 ': ' John ',}print (info) print (info[' stu002 ']) # Print output stu002 value info["stu002"]= "Zhang San" #修改值info ["stu004"]= "Heqian" #新增 #del info #删除字典i
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
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
The Python dictionary belongs to a data type, and we can store the data in a dictionary, which is defined using curly braces "{}".For example, now to store a person's information and then read it, you can use a slice of the way to reflect:In [1]: info =‘Tom 180 Male‘In [2]: info[1:3]Out[
Python study notes 7-advanced iterator, python study notes 7-
Evaluate any string as a python expression:
Eval () method:
Eval (source [, globals [, locals])-> valueEvaluate the source in the context of globals and locals.The source may be a string representing a
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.