python dictionary CRUD operations

Original link: http://www.cnblogs.com/wangtaobiu/p/9865274.html

A. Dictionary (key-value pairs)

  1. The basic dictionary format: {key1: 1, key2: 2}

  2. The dictionary must be immutable keys (such as: number, string, a tuple, bool value); value is variable, the available numbers, strings, lists, dictionaries and the like.

  3. The dictionary keys must be unique, can not be repeated

    (Python36 there is a print order, python35 and print the following versions are unordered)

  4 Operating dictionary:

    increase:

      1. The key change is performed by: (if the key does not exist is automatically added, if present, are modified) dic [key] = 'values'

      2.dic.setdefault ( 'key', 'values') is a bond before the comma, the value after the comma, if the default value is not added back to None. (Add not present, there is added)

    delete:

      1. Note that the dictionary does not remove method

      2.dic.pop () Note that the brackets must add a parameter, the parameter is to remove the key, pop returns a value, the return value is deleted.

       dic.popitem () to delete a random set of key-value pairs, python36 deleted is the last, python35 and randomly deleted before one pair

       dic.popitem () has a return value, the return value is a tuple key and the value thereof.

      3.del dic () to delete the entire dictionary

       del dic ( 'key') may be added in brackets to delete the specified key

      4.dic.clear () empty dictionary, you can not specify deleted

    change:

      1.dic [ 'key'] = 'value' modified by a bond (if present on the key to modify, add does not exist)

      2.update   update

         dic.update ({ 'key': ' value'})   is not a new key-value pairs will be added, have to be modified to add one or more of a plurality of modified

    check:

      1. Find by print (dic [ 'key']), if the key does not exist will be given

      2. By dic.get ( 'key') search, if there is no error, returns None.

. 1 DIC = {. 1: 2, ' . 3 ' :. 8, False: True, (l, 2,3): [ ' 132 ' , ' QW ' ], ' Duke ' : ' move mountains ' }
 2 S = DIC. GET (. 4, ' not present ' )    # is not given, the default can define their own return return value None 
. 3  Print (S)

 

  5. Other operations:

    1.print (dic.keys ()) print all the keys, print a list, but can not find the next table

    2.print (dic.values ​​()) print all the values ​​in a list

    3.print (dic.items ()) in the form of key-value tuple in a list

二.id  is  ==

  1.id () to view the memory addresses

    name = 'alex'

    print(id(name))

  2.is is viewed through is not the same memory address (compare that memory address)

  3. See == values ​​are equal, the equal sign (comparison value)

  4. Small data pool:

    1. Only numeric and string type of the role, the data pool is small in order to save memory, the plurality of code blocks can be used in small data pool

      Digital: -5 to 256 within this range points to the same memory address

      No string range

    2. If special characters, there is a small data pool

    3. Multiplication string can not exceed 20 characters, except 0 and 1

    

        

      

      

Reproduced in: https: //www.cnblogs.com/wangtaobiu/p/9865274.html

Guess you like

Origin blog.csdn.net/weixin_30215465/article/details/94958494