Basic Python tutorial for. Net programmers-[Third Day] For dictionary usage and basic python tutorial

Source: Internet
Author: User

Basic Python tutorial for. Net programmers-[Third Day] For dictionary usage and basic python tutorial

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. Basic usage of dictionaries.

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 >>> 'age' in dic # judge whether a Key exists. True >>> dic ['age'] = 22 # modify the value >>>> dic {'age': 22, 'name': 'frank'} >>> del dic ['age'] # delete an element >>> dic {'name': 'frank '}

3. dictionary formatting.
Two types of formatting are included in the preceding section. One is to format data using tuples, and the other is to format data using dictionaries using template templates. The following describes how to format data using dictionaries.

    

>>> workMsg = {'Frank':'Coder','Nancy':'HR','Vincent':'Project Manager'}>>> 'Frank is the %(Frank)s' %workMsg'Frank is the Coder'

2. Use of internal dictionary methods.

1. Clear method: when using the Clear method, you can refer to the following three examples.

Note: 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 no other space is generated. 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'}

2. fromkeys: Add a key with null values for the dictionary.

    

>>> {}.fromkeys(['Name','Age']){'Age': None, 'Name': None}}
>>> {}.fromkeys(['Name','Age'],'Unkown')
{'Age': 'Unkown', 'Name': 'Unkown'}
>>>

3. The has_key () and key in dic determine whether the key is included.
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'), ('Nancy', 'HR')]>>> 

5. After keys, iterkeys, and values and itervalues are implemented, go to the key List and values list.

6. pop (key), popitem, and del are used to delete field elements.

7. update (parma) One dictionary updates the other.

Iii. Summary:

    

In general, 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.