python_ dictionary dict Takeaways

dictionary:

  1, the basic feature dictionary

    dict is represented by {} key data: unique {key value}

    Hash keys must be immutable data types can be used as a dictionary key

    Value without any restrictions

  2, CRUD

  increase:

    dic[key] = value

    dic.setdefault (key, value) # If the key exists in the dictionary do nothing, or to add

                                                            # Can be queried by a key, this key does not return None

  ------------------------------------------------------------------------------------------------------------------

  delete:

    pop (key) # returns a value returned is the value to be deleted

    del dic[key]

    clear () # Empty dictionary

    popitem () # random deleted

  -------------------------------------------------------------------------------------------------------------------

  change:

    dic[key] = value

    dic.update (dictionary)

  -------------------------------------------------------------------------------------------------------------------

  check:

    dic.get(key)

    dic[key]

    for loop

    setdefault(key)

  -------------------------------------------------------------------------------------------------------------------

  3, other operations dictionary

    dic.keys acquired all [key] is present in a high copy list

    dic.values ​​all acquired values ​​[] a list of the presence of a high copy

    dic.items acquired all of the [key] to [] tuples present in the form of a list of a high imitation

    Deconstruction:

      a,b = 1,2

      a,b = (1,2)

      a,b = [1,2]

      a,b = "12"

  4, nested dictionary 

    DIC = { 
     "name": "Feng Wang",
     "Age": 43 is,
     "wife": {
     "name": "Zhang",
     "Age": 39,
     "the salary": 100000,
     },
     "Baby": [
     { "name": "boss", "Age": 18 is},
     { "name": "second child", "Age": 15}
     ],
     }

    DIC [ "Baby"] [0] [ "Age"] =. 19 # step by step to find and change the corresponding [value]
    print (dic)

Guess you like

Origin www.cnblogs.com/amilidi/p/12022557.html