{' sarlay ': 12000, ' name ': ' Zhangsan '}Find Value by keyCode: People = {' name ': ' Zhangsan '}print (people[' name ') ') Result: ZhangsanReturns all keys in the dictionaryCode dic = {' Tom ': One, ' Sam ': $, ' Lily ': 100}print (Dic.keys ()) results [' Lily ', ' Sam ', ' Tom ']Returns all the value in the dictionaryCode dic = {' Tom ': One, ' Sam ': $, ' Lily ': 100}print (Dic.values ()) Results [100, 57, 11]Set Set First, the concept:Fast access, can not be repeated, unorderedT
() Set the dictionary key-value pairs, group members, and finally return as a listDictionary operations: Keys (), VALUES ()Dictionary operation: Update (DICT1) updates the key-value pairs in the dictionary dict1 to the dictionaryDictionary operation:Items () An iterator that returns all elements of the dictionaryKeys () returns an iterator consisting of all keys
The following is a simple implementation of converting a json string into a dictionary in Python. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at a friend's demand today:
Lailai
{'Isok': 1, 'isrunning': None, 'iserror': None}
How to convert to dictionary
Well, the json conversion is very simple. star
determine whether it can be used as a key value;Create a dictionary: {key:value}, or you can use the Dict () factory function to createA={' A ': 1, ' B ': 2}print aa=dict (a= ' C ', b=2) print Aprint a.get (' C ', ' not exist ')Dict () function is created so that the key is the default string, value is also the default, the number can be directly written;In the dictionary does not confirm whether there is
() is the return list in P2, which copies a copy of the memory, P3 in the generator, does not replicate within the P2 can be used D.iteritems ()6. Dictionary RestrictionsThe key of the dictionary cannot be duplicated, and can be hash, unordered7. Default DictionaryFrom collections Import DefaultdictD1 = defaultdict (list) #default初始化时, need to pass in a function, this function is also called the factory fu
Python dictionaries can store any type of object, such as strings, numbers, tuples ... Advantages: Easy to take value, fast speed1, create a dictionaryA dictionary consists of a key (key) and a corresponding value (value). Dictionaries are also referred to as associative arrays or hash tables. The basic syntax is as fo
): Determine if there is a key in the dictionary, it is recommended to use in and not in instead of+dic.keys () returns a list of keys in the dictionary+dic.items () Returns a list of the corresponding tuples of the key value+dic.iter* () Iteritems () Iterkeys () returns the iteration value instead of the list+dic.setdefault (Key,default=none) with set, if key exists return its value, if key does not exist
(dic name): Gets the number of key-value pairs for the dictionary.
Dic.keys (): Returns all keys in the dictionary.
Dic.values (): Returns all values in the dictionary.
Dic.items (): Converts the dictionary to Ganso.
Iv. Ganso
(11,22,33) function is the same as the list, and differs in that Y
Python implements the merge of two dictionaries (dict), pythondict
This article describes how to merge two dictionaries (dict) in Python. The specific method is as follows:
Two existing dictionaries, dict, are as follows:
dict1={1:[1,11,111],2:[2,22,222]}dict2={3:[3,33,333],
,b_list))>>> AA{' Baidu ': ' com ', ' www ': ' 163 ', ' 123 ': ' B ', ' com ': ' vedio ', ' cn ': ' OA '}The a_list list is a keyword straight in the new dictionary, while the B_list list is straight in the new dictionary.2. Get3. References and copies4. Insert (update), deleteCC = {' QQ ': ' qq.com '}>>> cc{' QQ ': ' qq.com '}>>> aa.update (CC)>>> AA{' Baidu ': ' com ', ' QQ ': ' qq.com ', ' www ': ' 163 '
1. Basic RequirementsWrite a program to query the Employee Information table to achieve the following functions:(1) Let the user enter no less than 3 characters to query employee information(2) Through employee number or employee personal information can be accurate or fuzzy query to employee information(3) Output employee information
2. Implementing Code and annotationsfirst, provide the TXT file for employee information:[Email protected]:/mnt/hgfs/py
' >>>{' Jack ': ' [email protected] ', ' Tom ': ' [email protected] '}List (d.keys ()) Returns an unordered list of all the keywords in a dictionarySorted (d.keys ()) Returns a sorted list of all the keywords in a dictionaryThe Dict () constructor can create a dictionary directly from the key-value pairDict ([' Tim ', 123), (' Tiny ', 234)]) >>>{' Tiny ': 234, ' Tim ': 123}Derivation creates a dictionary:{
', ' class ': None}Get #用于访问字典中的项, even if the item does not exist, can also customize the default return value>>> C{' name ': ' Sola ', ' class ': None}>>> c.get (' name ')' Sola '>>> c.get (' home ')>>> c.get (' home ', ' Meizhou ')' Meizhou 'Has_key #检查字典中是否含有特定的键. Similar member methods>>> C{' name ': ' Sola ', ' class ': None}>>> c.has_key (' name ')True>>> C.has_key (' home ')FalseItems,iteritems #items Returns all entries in the dictionary as
1. Use of dictionaries# 1. Define a dictionary that uses a, B, C, D as the dictionary's keywords, with values of any content Dict1= {' A‘:' AA‘,' B‘: ' Xyz " c ' Helo " ': 123}# 2. After adding an element ' C ': ' Cake ' to the dictionary, output the dictionary to the screen # the key of the
member of T, that is, an element that returns s different from tS.symmetric_defference (t) returns the collection of all S and T exclusive (non-co-owned) elementsS.copy () returns a shallow copy of S, which is more efficient than a factoryMethod (for mutable collections only): The following method parameters must be hashedS.update (t): modify S with elements in T, i.e. s now contains members of S or TS.intersection_update (t): Members in S are elements that collectively belong to S and TS.diffe
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.