1. DictionariesDictionary, one of the underlying data types, {} stores data as a key-value pair.Store data in key:value form. For example, name is Key,laonanhai as value.DIC = {' name ': ' Laonanhai ', ' age ': ' name_list ': [' negative ', ' junny ', ...] }The only type of mapping in Python. Used to store large amounts of relational data.Classification of data types:Variable class:Variab
In Python, metadata groups, lists, Dictionary differences, and python dictionaries
Python has three built-in data structures: List, tuples, and dictionary.1. ListList is the data structure that processes a group of ordered projects. You can store a series project in a list.
mapping type can be used as a key for other object types (e.g. numbers, strings, ganso, usually strings as keys), and the keys of the sequence type are different, the key of the mapping type is straight
4. Connect to or indirectly associate with the stored data value.
5. The data in the mapping type is unordered. This is not the same as the sequence type, and the sequence type is numerically ordered.
6. The mapping type is directly "mapped" to a
Python dictionaryis an unordered, data type stored in key-value pairs , with strong data affinity and a unique mapping data type. key: Must be a hash (immutable data type: string, number, tuple, bool) value and is uniqueNone: None is an empty object, ' is a string object, none is a special data typeDictionary method:dic={"Name" = ' Alex ', ' sex ' = ' men '} up =
, 9, 12, 15, 18, 21, 24, 27]
Example 2:
def squared(x): return x*xmultiples = [squared(i) for i in range(30) if i % 3 is 0]print multiples# Output: [0, 9, 36, 81, 144, 225, 324, 441, 576, 729]
2. Generate a generator using ()
Change the two table's deduced [] to () to get the generator.
multiples = (i for i in range(30) if i % 3 is 0)print(type(multiples))# Output:
Ii. dictionary Derivation
present in the dictionary (note that the default value for parameter default is None)
Dict.has_key (Key)
Returns true if the key is present in the dictionary, or false. After the Python2.2 version is introduced in and not, this method is almost obsolete, but still provides a working interface.
Dict.items ()
Returns a list containi
'
The acquisition of value in a dictionary
If key does not exist, then Dict[key] throws an error, sometimes in order to avoid the error, we use the Get () function to obtain the value of the key, if the key does not exist, then the default returns None
Animals.get (1)
' Tiger '
Animals.get (4, ' default ')
' Default '
) the items () function: Gets all the items of a function
For Eachitem in Dict1.items (): print (eachitem,end= "")
(5) The Get () function gets the value corresponding to the dictionary key and returns none if it does not exist
Str1=dict1.get (+) print (STR1)
(6) In or not in member relations: Judging whether a k
Dict Dictionary TypesDict Dictionary type provides methods: (Total 9 kinds)1. Clear the DictionaryDic.clear ()2, copy the dictionary shallow copyDic.copy ()3. Create a dictionary based on the sequence and specify a uniform value (none by default); This method is a static met
Dictionary in Python
The field is the only key-value type in the dictionary in Python. It is a very important data structure in Python. because it uses hash to store data, its complexity is O (1 ), fast. The following lists the co
throws an exception, two 0 elements, also only delete one. >>> d{' A ': 1, ' C ': 1, ' d ': 0} >>> D = {' A ': 1, ' B ': 0, ' C ': 1, ' d ': 0} #d. Keys () is an array of subscripts >>> D.keys ( [' A ', ' C ', ' B ', ' d '] #这样遍历, no problem, because actually this is the D.keys () this list constant. >>> for K in D.keys (): ... if d[k] = = 0: ... del (d[k]) ...>>> d{' A ': 1, ' C ': 1} #结果也是对的 >>>
In fact, this problem is very simple, that is, if you traverse a
an entry with a key of K.
(3) The Copy method returns a new dictionary with the same key-value pair.
(4) Fromkeys: Method uses the given key to create a new dictionary, each key corresponding to the value of none.
print {}.fromkeys ([' Name ', ' age ']) output: {' age ': none, ' Name ': none}
(5)
Example of how to use the python command line youdao dictionary, python command line
Preface
Since I have been using the Linux system, it is particularly difficult to support dictionaries. For those of me who have been reading English documents, I have been stuck in shells and used a proper dictionary before, I feel ve
Fourth dictionary: When the index is not good1. A data structure that refers to a value by its name , which is called mapping , and the dictionary is the only built-in mapping type in Python.2.len (): You can return the number of key-value pairs in the dictionary.The 3.del k
Dictionary data type (other programming languages may be called associated arrays or hash arrays)
Index linked list VS Dictionary (omitted)
Python dictionary
# Initializing an empty dictionary
Pos = {}
# Other dictionary usag
(dict2,10) print (DICT4) #字典 (Dictionary) get () function returns the value of the specified key if the value does not return a default value in the dictionary. dict5={' name ': ' Gyh ', ' Age ':25}print (dict5.
(): if value == value_to_get: print key
Result: 83682
That is, the abcdef encoding in the dictionary.
I hope this article will help you with Python programming.
How to sort Python dictionaries, for example
In Python2.7.x, OrderedDict is added to the collections class. The usage is as follows: In Python2.7.x, Ordere
= (' Tom ', ' 30 ')Dict1 = Zip (tuple1,tuple2)Dict1 = Dict (Dict1)3, the method of the dictionary1,clear () empty Dictionary Dict1 = {' name ': ' Tom ', ' age ': ' All '}dict1.clear () run result: {}2,copy () copy dictionary dict1 = {' name ': ' Tom ', ' age: '}dict2 = dict1.copy () print dic run result: {' age ': ' ' name ': ' Tom '} 3,fromkeys is primari
Today to summarize some of the methods of operation of the python3.4 version dictionary. A dictionary is an unordered storage structure inside Python that stores key-value pairs of Key-value. Keywords should be immutable types, such as strings, integers, tuples containing i
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.