"^_^" then open the Python command line interpreter and import mdict
>>> From mdict import myDict>>> D = myDict ({1: 'A', 2: 'B', 3: 'C '})>>> D{1: 'A', 2: 'B', 3: 'C '}>>> D [1]'A'>>> D [4]_ Missing _ called, key = 4 then _^ You Can See That _ missing _ () is called.
If you only want to get the value corresponding to a key and do not want to change it, use the
Dictionary # In Python, the dictionary is a series of key-value pairs. # key-value pairs are separated by:, and key-value pairs are separated
Python learning
Python multi-dimensional/nested Dictionary data infinite traversal implementation, python multi-dimensional
Recently I picked up Django and learned how to traverse multi-dimensional Dictionary data in the instance practice. Google does not query related information... After all, I was a newbie. When I started my work,
unique
? B. The key in the dictionary must be immutable
? List is mutable and cannot function as key
? A tuple is immutable and can act as a key, a numeric type, and a string can act as key
2. Create:
syntax: dictionary name = {key1:value1,key2:value2 ...}
Code Demo:
#1. Creating a Dictionary
#创建一个空字典
Dict1 = {}
#创建有键值对的字
': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}Dic.pop (' K5 ')Print (DIC)7. Please get the value of ' K2 ' in the dictionarydic={' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}Dic.get (' K2 ')Print (Dic.get (' K2 '))8. Please get the value of ' K6 ' in the dictionary, if the ke
The 1.dictionary:dictionary type is a dictionary that represents a key/value repository.given a Dictionary object that searches for the value corresponding to the key, the dictionary is considered a list of key/value pairs. 2. Typ
' } A.update (b) print (a) {' name ': ' DEMONLG ', ' 2222 ': ' 32313 '}
clear
empty list All contents
a = { ' name ' : " DEMONLG ' } B = { ' 2222 ' : ' 32313 ' } A.clear () print (a) get: {}
setdefault
Returns the value corresponding to the specified key, adds a new element without a corresponding
used for initialization, calling the Add method to initialize the Trie tree, which is based on each string. The Add method adds each character to the child node, if it exists, it is shared and the next child node is found, and so on. Find is for finding whether a child node that stores a character has been established, and preorder is the word that gets the store first. Tree traversal has three kinds: first-order traversal, in-sequence traversal and subsequent traversal, if you do not understan
Dictionary键key:拼音值value:页码key-value:键值对字典是python中唯一的映射类型,指两个元素之间一一对应的关系(注明:字典是映射类型,不是序列类型)brand=[‘外星人‘,‘戴尔’,‘联想’,‘苹果’]English=[‘AlienWare‘,‘Dell‘,‘Lenovo‘,‘Apple‘]#品牌与英文一一对应print(‘外星人---‘,‘AlienWare‘)显示不出来中文,ASCII是十进制, 此时utf-8用的是Unicode,对应的是十六进制的数据,此时转换过程#不行,byteString,十进制,unicodeString 十六进制,python2默认十进制,• Creation of
The Python Dictionary {} Saves the data as a key-value pair and can access the saved values in the dictionary with the keys instead of the subscript. Dictionaries can contain almost any number of variables, dictionaries, sequences, and tuples. The sequence is the same.The list of P
the keys of the dictionary.‘‘‘Seq={' a ': ' One ', ' B ': ' A ', ' C ': ' + ', ' d ': ' 44 '}Dict=dict.fromkeys (seq)Print DictPrint Dict.fromkeys (seq,20)---------------------------------------------{' A ': none, ' C ': None, ' B ': none, ' d ': none}{' A ': +, ' C ': +, ' B ': +, ' d ': 20}-----------------------------------------------------------------------------------‘‘‘4.get (): Returns the
][2].get (' K1 ') [1] = ' # ' # (2)print (LIS)#10.3 Change the list from ' 1 ' to Digital 101 (two ways)#lis [0][1][2][' k1 '] = [' TT ', ' 3 ', 101] (1)lis[0][1][2].get (' K1 ') [2] = ' 101 ' # (2)print (LIS)#11, there is now a list of li = [All-in-one, ' a ', ' B ', ' 4 ', ' C '], there is a dictionary (this dictionary
= Execution Result: 5print (' Al ex Lil '. Split (' L ')) #以l作为分隔符 + = execution Result: [' a ', ' ex ', ' I ', ']Print (' 1+2+3+4 '. Split (' \ n ')) #以换行符分隔 = = Execution Result: [' 1+2+3+4 ']print (' 1+2\n+3+4 '. Splitlines ()) #识别换行符分隔 = = Execution Result: [' 1+2 ', ' +3+4 ']print (' Alex Li '. swapcase ()) #大写变小写, lowercase to uppercase = + Execution Result: Alex Liprint (' Alex Li '. Title ()) #变成主题 = = Results of the execution: Alex Liprint (' Alex Li '. Zfill ()) #没啥卵用 and results: 000
(shallow copy)(3) Fromkeys ():V=dict.fromkeys (['key','key2','key3 '],123)print(v)Code Execution Results:The method is used to initialize a dictionary, the element in the first parameter as the key value, if passed in a list, each element in the list will be the key value, the second parameter is the value of all key
Python programming to extract the subset from the dictionary for analysis, python subset
This example describes how to extract a subset from a dictionary using Python programming. We will share this with you for your reference. The details are as follows:
First, we will cons
;>: , : [, ]}
From the code above, we can see that the original dictionary is not affected when the value is replaced in the copy. However, if a value is modified, the original dictionary will also change.
One way to avoid this problem is to use deep replication to copy all the values it contains. You can use the deepc
: count.setdefault(word, 0) += 1 return count print word_count(['hiiragi', 'kagami', 'hiiragi', 'tukasa', 'yosimizu', 'kagami'])
When you start the above code with great enthusiasm, the code will immediately throw the exception to your nose-because it appears on the count on the left of the ++ = operator. setdefault (word, 0) is not a left value in Python. now let's
'] = 456Print(obj)= = "{' K1 ': 123, ' K2 ': 456}Python single-instance mode#Python Single-instance mode#1. Using the class method to implement#2. Using static fields to implementclassfoo:instance=Nonedef __init__(self,name): Self.name=name @classmethoddefget_instance (CLS):#CLS class name ifCls.instance:#If the static field instance is True returncls.instanceElse: obj= CLS ('Alex') Cls.in
V value for inch info.values (): Print (i)3.6 Items() function: Take k-v value, use two iteration variables to loop k,v for inch Info.items (): Print (K,V)3.7 Get(d=none) function: safe accessor, no error when fetch K is not presentv = info.get ('k',"ERROR") # The error is the value returned when it is not p
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.