Basic Python tutorial for. Net programmers-usage of dictionaries [Third Day]

Source: Internet
Author: User

Basic Python tutorial for. Net programmers-usage of dictionaries [Third Day]
Today I learned how to use the dictionary. The so-called dictionary is actually Key-value pair data. A dictionary has a unique Key that corresponds to a Value. The Key is unique and the value is not unique. in. net will report an error when adding the same Key. in Python, if the same Key appears, the value of the subsequent Key will overwrite the previous data. i. the basic usage of the dictionary. 1. create a dictionary: The field creation format is {key: Value, Key: Value}. You can also convert it using the dict function. Note: Keys are sorted in ascending order of strings. If the dictionary is declared with the same key, take the last one. (Different from. net) >>>> dic1 = {'name': 'hard', 'age': 24 }>>> dic1 {'age': 24, 'name ': 'bad' }>>> imtes = [('name', 'frank'), ('age', 23)] >>> dic = dict (imtes); >>> dic {'age': 23, 'name': 'Frank }>>> dic2 = {'name': 'hard ', 'age': 24, 'name': 'frank'} # The first one is overwritten >>> dic2 {'age': 24, 'name ': 'frank'} 2. basic usage of dictionaries >>> imtes = [('name', 'frank'), ('age', 23)] >>> dic = dict (imtes); >>> dic {'age': 23, 'name': 'frank' }>>> len (dic) # length 2> 'a Ge 'in dic # True> dic ['age'] = 22 # modify value> dic {'age': 22, 'name ': 'frank' }>>> del dic ['age'] # delete an element >>>> dic {'name': 'frank'} 3. format the dictionary. two types of data are formatted in total. One is to format data through tuples, and the other is to format data using dictionaries using template templates. the following describes how to format data in a dictionary. >>> workMsg = {'frank': 'CODER', 'nancy ': 'hr', 'Vincent': 'Project Manager'} >>> 'Frank is the % (Frank) s '% workMsg' Frank is the Coder 'II. provides internal usage of the dictionary. 1. clear method: when using the Clear method, you can see the following three examples. not E: data is cleared by dic ={} on the surface. Case 1: Both dicTestB1 and dicTestA1 point to the same space. However, the dicTestA1 = {} Operation actually opened up a new space. dicTest1 points to the space corresponding to {}, so dicTestB1 retains the original data. case 2: dicTestB2 and dicTestA1 point to the same space. however, dicTestA1.clear () clears the current space and does not generate any new space. Therefore, dicTestB2 has no data. case 3: Because the copy () method is called, a new space is opened when dicTestB3 = dicTestA3.copy (), so dicTestB3 and dicTestA3 are irrelevant, therefore, dicTestA3 does not affect the value of dictex333. >>> dicTestA1 = {'name ': 'frank' >>>> dicTestB1 = dicTestA1 >>>> dicTest1 ={}>> dicTest1 {}>>> dicTestB1 {'name': 'frank' }>>> >>> DicTestA2 = {'name': 'frank'} >>> dicTestB2 = dicTestA2 >>> dicTestA2.clear () >>> dicTestB2 {}>>>>> dicTestA3 = {'name': 'frank' >>>> dicTestB3 = dicTestA3 >>> dicTestB3 = dicTestA3.copy () >>> dicTestA3.clear () >>> dicTestA3 ={}>>> dicTestB3 {'name': 'frank'} copy Code 2. fromkeys: Add a null key for the dictionary. >>> {}. fromkeys (['name', 'age']) {'age': None, 'name': None }>> {}. fromkeys (['name', 'age'], 'unkown ') {'age': 'unkown', 'n' Ame': 'unkown '} >>> 3. has_key () is the same as key in dic to determine whether it contains key 4. items and iteritems: Get the dictionary element list for dictionary traversal. The class is in. net keyvaluepair <key, value>, which is implemented by the iterator. generally, the iterator is more efficient. >>> workMsg = {'frank': 'CODER', 'nancy ': 'hr', 'Vincent': 'Project Manager'} >>> workMsg. items () [('frank', 'CODER'), ('Vincent ', 'Project Manager'), ('nancy', 'hr')]> list (workMsg. iteritems () [('frank', 'CODER'), ('Vincent ', 'Project Manager'), ('nanc Y', 'hr')] >>> 5. keys, iterkeys, values, and itervalues are implemented to go to the key List and values list respectively. 6.pop( key), popitem, and del are used to delete field elements. 7. update (parma) One dictionary updates another dictionary. III. conclusion: The dictionary is quite simple. however, the clear () and update () Methods used in the call are all operations on the data in the current memory. If you assign values through = alone, the update effect can be achieved. In fact, the principle is different, = is equivalent to discarding the previous data and storing it in the new memory. This is a bit of a class because we often update the database data, which can be implemented through update, or the same principle can be achieved through delete and add.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.