Python study notes fifth day

 Python study notes fifth day

1. Knowledge Review

   1.1 list operations:

         1.1.1 by:

               1.1.1.1 append () at the end of the new default

              1.1.1.2 insert()

              1.1.1.3 extend()

                        1.1.1.3.1 action: time increase in the value of another plurality of sequences at the end of the list, a list of commonly used as expansion of original

                       1.1.1.3.2 Syntax: list.extend (seq)

                                       seq: lists, tuples, sets, dictionaries

                                     Note: Should a dictionary, as a key element in turn will only add to the original end of the list

        1.1.2 deleted:

               1.1.2.1 pop()

1.1.2.2 remove()

1.1.2.3 of ()

1.1.2.4 clear (): used to clear the list

  Other common operations 1.2

1.2.1 count (object name) statistics

1.2.2 len (object name) length

1.2.3 sort () to sort

Note: Sorting reverse = False (default ascending order)

reverse = True (the default descending order)

       1.2.4 Nested problem

                 Internal elements can not be modified tuple, but the interior element itself is a variable object, the variable internal object can be modified.

Day notes as follows:

Tuple element can not be modified, but if a variable which forms the nested list, etc., then this form of the variable element may be modified.

In other words, not only for denatured tuple subelements, subelements that should the internal variable object, then the child element of the child element in the tuple is variable.

 

2. The learning content

   Dictionary 2.1

      2.1.1 Definition:

              The only use the mapping to express, to take "key-value" in the form of storage

               Note: key must be hash (hash), i.e., immutable type, the saved data is not stored in the order of artificial and is stored in the order of the hash table , the hash table Refer to the following links (taken from Baidu Wikipedia - hash table): https://baike.baidu.com/item/%E5%93%88%E5%B8%8C%E8%A1%A8/5981869?fr=aladdin

           Summary: immutable data types:

                     tuple string string integer tuple of type int boolean bool

                     Variable data type:

                     Dictionary dict collection set in list

 

      2.1.2 Other operating Dictionary

                 2.1.2.1 increase

                            Method One: Assigning Method

                                      Dictionary name = {}

                                      Name dictionary [ 'Key (key name)'] = 'value (value)'

                                      Note: If this key does not exist to the dictionary, it will automatically add the key-value pairs

                            Method two: the default setting method

                                      Dictionary name .setdefault ( 'key (key name)', 'value (value)')

                                     Note: 1. When using setdefault, if the key name already exists, will not work when you re-add, that is, when you set the default value, if it already exists in the dictionary, it will not work.

                                                2. If only one key name, and did not enter a value that is not only a key value, it is displayed in the dictionary: key name: None

 

               2.1.2.2 delete

                            Method One: Remove by key

                                          del dictionary name [ "key (key name)"]

                                         Dictionary name .pop ( "key (key name)")

                           Method Two: random delete

                                         Dictionary name .popitem (): not specified, randomly deleted

                          Method three: empty dictionary

                                         Dictionary name .clear (): Empty the contents of the dictionary

 

              2.1.2.3 change

                            Dictionary Name [Key (key name)] = 'new value (value)'

                           Merge operations

                              Dictionary name .update (another name dictionary)

                       Note: If two dictionary key name (key) the same name, the new value (value) will be covered by the old value

 

              2.1.2.4 check

                         Method One: Direct inquiries

                                       print (name dictionary [ 'Key (key name)']

                        Method Two: Use the get method

                                      print (dictionary name .get ( 'key (key name)')

                          Note: Use this method to query value does not exist

1 .print ( dictionary name .get ( 'key (key name)') will return

None , but he does not complain.

2. If the second parameter is set, i.e.,

print ( dictionary name .get ( 'key (key name) "," two parameters ")

Two input parameters is returned

 

               2.1.2.5 Other related operations

                          2.1.2.5.1 traversal operation

                                      2.1.2.5.1.1 get all the key (key name)

                                                      Method a: using a for loop

                                                                      for i in the dictionary name .keys ():

                                                                                print(i)

                                                       Method Two: Direct call

                                                                        print (dictionary name .keys ())

                                      2.1.2.5.1.2 get all values ​​(value)

                                                       Method a: using a for loop

for i in the dictionary name .values ​​():

print(i)

Method Two: Direct call

print (dictionary name .values ​​())

                                   2.1.2.5.1.3 direct traversal.

                                                      Method One: Direct call

print (dictionary name)

                                                      Option two: function

                                                                     print (dictionary name .items ())

                                                                   Note: The output will be output in the form of tuples

                                                     Method three: Use a for loop to get the key

for key, value in the dictionary name .item ():

print("-->",key,value)

 

                          2.1.2.5.2 structure, unpack

A parameter, the parameter value of two = 1, the value 2

A parameter, two parameters, three parameters = ( "value 1", "value 2", "the value 3")

print (a parameter)

......

                          2.1.2.5.3 dictionary nesting

                                       Please visit this section of code, not repeat them

                                     Involving nested query, according to their own code appreciated

= {myDict ' ZTE ' : { ' Name ' : ' Hou ' , ' Job ' : ' founder ' , ' Age ' : 74}, ' Ali Baba ' ( ' Internet company ' , ' Son ' ), ' Tencent ' : [ ' glory of the king ' , ' to stimulate the battlefield ' , ' League of Legends ' , 'Cross Fire ' ]," Baidu " : " People look for thousands of Baidu TA ' }
 Print (myDict)
 Print (mydict.get ( ' ZTE ' ) .get ( ' name ' ))
 Print (mydict.get ( ' Tencent ' ) [0])
 Print ( mydict.get ( ' Ali Baba ' ) [1 ])

 

                                   operation result

 

{ 'ZTE': { 'name': 'Hou', 'Job': 'founder', 'age': 74}, 'Ali Baba': ( 'Internet company', 'Son'), 'Tencent' : [ 'glory of the king', 'to stimulate the battlefield,' 'League', 'cross Fire'], 'Baidu': 'People look for TA thousands of Baidu'}

Hou

King of glory

Masayoshi Son

 

Guess you like

Origin www.cnblogs.com/tjlhappyboy/p/11209681.html