#__author: "Feng Lin"#DATE:2018/8/25#Increase:dict1={'name':'Lin',' Age': 21,'Sex':'Mans'}#Add with Keydict1['hight']=185Print(Dict1)#no keys are covereddict1[' Age']=24Print(Dict1)#presence key, no change, no addDict1.setdefault ('Weight')Print(Dict1) Dict1.setdefault ('Weight', 150) Dict1.setdefault ('Fat', 150)Print(Dict1)#Delete, with return value, can be set by defaultPrint(Dict1.pop ('Lalal','no This key'))#Delete return key valuePrint(Dict1.pop ('name','no This key'))#randomly deletes a k
1. Create an empty dictionarytype(dic)type ‘dict‘>2. Direct Assignment creation>>> dic = {‘spam‘:1‘egg‘:2‘bar‘:3}>>> dic{‘bar‘3‘egg‘2‘spam‘1}3. Create by keyword dict and keyword parameters>>> 12, bar =3)>>> dic{‘bar‘3‘egg‘2‘spam‘1}4. Create by two-tuple list>>> list = [(‘spam‘1), (‘egg‘2), (‘bar‘3)]>>> dic = dict(list)>>> dic{‘bar‘3‘egg‘2‘spam‘1}5.dict and zip combine to create>>> dic = dict(zip(‘abc‘, [123]))>>> dic{‘a‘1‘c‘3‘b‘2}6. Create by Dictionary
How to Use the python dictionary for multi-key and repeated key values (detailed description), python dictionary
Use a dictionary in Python in the following format:
dict={ key1:value1 , key2;value2 ...}
The usage format for actual
Check out the Python official documentation version 3.6 prior to learning about the following knowledge.1, Range (start,stop,step) function. Start defaults to 0,step to take a number every few steps.List[range (5)]=[0,1,2,3,4]The integer within the generated range. List[range (1,5)]=[1,2,3,4]List[range (1,5,2)]=[1,3]2, Randint (b) generates a random integer between a B (including a B). A=randint (3,6) a=3|4|5|63. generate a list with a length of 10, w
Functions used in the classic dictionaryDict: Create a dictionary through a sequence pair such as other ing (such as other dictionaries) or (Key, value. Of course, dict is not very definite as a function. It is essentially a type. Like list.
Copy codeThe Code is as follows:Items = [('name', 'zhang '), ('age', 42)]D = dict (items)D ['name']
Len (d): number of returned itemsD [k]: The value above the return k
1, the basic derivation type
(1) List derivation is one of the most popular features of Python, and it can be used to construct new lists very efficiently: just a simple piece of code that filters the family elements and distorts the resulting elements with the following basic grammatical structure:
[Exp for Val in collection if condition] #列表推导式
The code above corresponds to the following loop:
result = []
...: For Val in collection:
...:
Python nested dictionary comparison value and value implementation example, python nested dictionary example
Preface
This article provides examples to show you how to compare the values and values of the python nested dictionary a
This article illustrates how Python simply traverses the dictionary and deletes elements. Share to everyone for your reference, specific as follows:
There must be a problem with this approach:
D = {' A ': 1, ' B ': 2, ' C ': 3} for
key in D:
D.pop (Key)
will report this error:runtimeerror:dictionary changed size during iteration
This way Python2 feasible, Python3 or reported the above error.
3.4.6 Items and Iteritems
Description: Items returns a key-value pair in a dictionary as a list, and Iteritems returns a key-value pair with an iterator object (no longer supported in Python3);
Example:1: >>> x 2: {' name ': ' Bill '} 3: >>> x.items () 4: dict_items ([' Name ', ' Bill ')] 5: 6: #python3中不再包含iteritems 7: >>> x.iteritems () 8:traceback (most recent Call last): 9:file "10:x.iteritems ()11:attributeerror: ' d ICT ' object h
Tuple a = (1,2,3,[1,2,3) tuples cannot modify the list of available nested lists such as (*. A]) can be modified generally not usedList A = [+/-A]The maximum feature of set set a = {one-way} will automatically go from, so he is not ordered, nor does it support slicing,The dictionary dict is basically the same as the set, which is the form of the Key:value key-value pair A = {"A": 1, "B": 2} is a JSON-formatted data pairTuple lists, the conversion of c
listIndex () #查看某一元素的索引号, no error foundExtend () #扩展列表L.extend ([i]) #将列表 [All-in-one] append to the list L, merged into a list,Remove () #删除元素 by default (that is, delete from the left)L.remove (' A2 ') #指定元素删除Sort () #排序, from small to large, no return valueReverse () #反转操作, no comparison [1,2,3,4]>>[4,3,2,1]Two, the meta-groupDefinition: Similar to a list, except [] Change to ()Characteristics:1. Can hold multiple values, the elements of a tuple can be any data type2. Immutable3. Define the
The data is displayed as:rows=[{' date ': ' 2018-09-04 ', ' Test 1 ': ' 50.00 ', ' Test 2 ': ' 100% '}, {' Date ': ' 2018-09-05 ', ' Test 1 ': ' 100% ', ' Test 2 ': ' No Execute '}, {' Date ': ' 2018-09-06 ', ' Test 1 ': ' 100% ', ' Test 2 ': ' 100% '}, {' Date ': ' 2018-08-31 ', ' Test 1 ': ' No execution ', ' Test 2 ': ' No Execution '}, {' Date ': ' 2018-09-01 ', ' Test 1 ': ' No execution ', ' Test 2 ': ' No Execute '}, {' Date ': ' 2018-09-02 ', ' Test 1 ': ' No execution ', ' Test 2 ': ' N
want a particular order, then you should sort them before using them.
Instance:
#coding = UTF-8 #author: Rxs002dict1 = {' Zhang ': ' hellos ', ' Wang ': ' Wangbaoqiang ', ' li ': ' Bingbing ', ' Zhao ': ' Vicki '} #字典的操作, add, delete, print dict1[' Huang '] = ' Huang Jiaju ' del dict1[' Zhao ']for firstname,name in Dict1.item (): Print Firstname,name
Execution Result:
Li Bingbing Wang Wangbaoqiang Huang Huang Jiaju Zhang Hellos
Summarize
This is about
dict.itervalues () returns a dictionary of value iterations Dict.pop (Key[,default]) and a method get () similar to if the key key exists in the dictionary, deletes and returns Dict[key], Throws a Keyerror exception if the key key does not exist and does not give the default value. Dict.popitem () randomly deletes a key from the dictionary-value is similar
#看看字典怎么个传法db = {' name ': ' Toby ', ' age ': +, ' phone ': ' 119 '}def Show (**dicts): for key in Dicts:print (Key,dicts[key]) s How (**DB) #接受字符串, tuples, lists, dictionaries (Python is too tmd) db = {' name ': ' Tantianran ', ' age ': ' Phone ': ' 15915822634 '}def show (AG, *args , **dicts): Print (' AG: ', ag) print (' args: ', args) for key in Dicts:print (Key,dicts[key]) show ("Hello word!" , 520,988,
DefinedUsing key-value pairs,>>> person = {"name":"Keven"," Age": 15,"Gender":"male"}>>> person['name']'Keven'>>>type (person)'Dict'>A dictionary can be modified in place, that is, it is mutable.>>> dict1={}>>> ID (dict1)139736380198256>>> dict1["name "keven">>> ID (dict1)139736380198256>>> dict1{'name' 'keven'}Using tuples to construct dictionaries, the method is as follows:>>> name = ([" First","Google"
Python implementation to add entries to the dictionary, python dictionary
This example describes how to add entries to a dictionary using python. It is a practical technique for dictionary
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.