Python dictionary operations and python dictionary operationsToday, I plan to write something every week to record my learning and mark it myself.
The dictionary is the most flexible built-in data structure in python. The
1. Dictionary: in Python, a dictionary is a series of key-value pairs. Each key is associated with a value, and you can use the key to access the value associated with it. The values associated with the key can be numbers, strings, lists, and dictionaries. You can actually use any
Scenario: Count the frequency of occurrences of a word in an article, and then sortUse the sorted function to return a sorted list, but not change the original data structureIn [1]: dt = {'a': 3,'b': 2,'C': 1}in [2]: Sorted (Dt.items (), key=LambdaD:d[1]) out[2]: [('C', 1), ('b', 2), ('a', 3)]in [3]: DT#DT itself has not changedOut[3]: {'a': 3,'b': 2,'C': 1}in [4]: Sorted (Dt.items (), key=LambdaD:d[1],reverse=True) out[4]: [('a', 3), ('b', 2), ('C', 1)]in [5]: dt = {'a':{'Val': 3},'b':{'Val': 5
>>> people = {"Tom": 175, "Jack": "Kite": "White": "#定义一个字典>>> for name in people: #获取value的值Print People[name]180160175170>>> for name in people: #获取key的值Print NameWhiteKiteJackTom>>> People.keys () # Gets the value of all keys, just like the For loop results above[' White ', ' Kite ', ' Jack ', ' Tom ']>>> people.values () # Gets the value of all values, as in the For loop result above[180, 160, 175, 170]>>> People.has_key ("white") #判断字典中是否存在该键True>>> people["Tom"] #通过key去获取value170>>> print
This article mainly introduces the Python learning tips of the use of the dictionary of the default behavior of the relevant information, the text is described in very detailed, for everyone has a certain reference learning value, the need for friends below to see it together.
This article is about Python using the dictionar
1. The dictionary is the only ing type in python. It stores data in the form of key-value pairs. Python performs hash function operations on the key and determines the storage address of the value based on the calculation result. Therefore, the dictionary is unordered and the key must be hashable. It can be hashed to i
Python-dictionary and python dictionary
A dictionary is a key-value data type. You can use strokes and letters to View Details of the corresponding page.
Features:1. dictionary is not required. (If the strings in the
I have used the Xinhua Dictionary. How can I sort the dictionary using the python language? The following describes how to sort the dictionary by value by using Python in this tutorial. For details, refer to the following:
Sort the
Dictionary
We have all used language dictionaries to find definitions of words that we don't know. A language dictionary provides a standard set of information for a given word, such as Python. This system associates the definition and other information with the actual word (map). Use words as key locators to find inf
There are generally two common practices:The first approach: using a self-implemented function:There is a Has_key () method in the property method of the Python dictionary:[Python]View PlainCopy
#生成一个字典
D = {' name ': Tom, ' age ':ten, ' Tel ':
#打印返回值
Print D.has_key (' name ')
#结果返回True
The second method: Use the In method:[
}, and age ' {age} ') #替换print ("Alex Li, Chinese name is Lijie" . replace ("Li", "Li", 1))
#大小写互换str = "This is string EXAMPLE....WOW!!!" Print (Str.swapcase ()) print (Msg.zfill (+)) # Out:00000my name is {name}, and age is {Age}print (Msg.ljust (+, "-")) #my Nam E is {name}, and age was {age}-----Print (Msg.rjust (+, "-")) #-----My name is {name}, and ' age ' {age}# detects if a string can be used as a marker, that is, whether Conforming variable naming rules b= "ddefdsdff_ haha" print (B.isi
Python has won a lot of programmers ' pro-gaze with its beautiful syntax and convenient built-in data structures.
There is a very useful data structure, which is the dictionary (dict), which is very simple to use. When it comes to traversing a dict structure, I think most people will think of the for-key in Dictobj method, and it is true that this method is applicable in most cases. But not completely safe,
Python basics 2-dictionary, python2-dictionary
Dictionary
Dictionary is another variable container model that can store any type of objects.
Each key-value (key => value) pair of the dictionary uses the colon (:), And each pair is
Foreplay: The data series in Python is divided into variable (mutable) and immutable (immutable) twoImmutable: string, int, float, tupleFeature: The same object occupies only one memory address, regardless of how many variables refer to it, such as A=1,b=1Because it is immutable, each time a new object must be created, the Python garbage collection mechanism will automatically clean up if no references poin
The following small series for everyone to bring a brief discussion of Python in the list, string, Dictionary of common operations. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
The list operation is this:
A = ["haha", "Xixi", "Baba"]
Add: A.append[gg]
A.INSERT[1,GG] in place labeled 1, added GG
Delete: a.remove (h
Dictionary1. Querying memory AddressesA = 10Print (ID (a))b = APrint (ID (b))b = 15Print (ID (b))2. Data typeImmutable types: Integer, string, tupleMutable type: list, dictionaryThe value of a dictionary can be any type, and the key can only be an immutable type.The dictionary store is unordered. For example:DIC = {' name ': ' YSW ', ' age ': $, ' hobby ': ' Game ', ' Is_handsome ': True}Print (DIC)Print (d
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.