Root of all evil -python basic data types

Root of all evil - basic data type (dict) main content of this section:

  1. The brief dictionary
  2. Dictionary CRUD operations and other nested dictionaries 3.
    ⼀ a. Brief dictionary
    dictionary (dict) is a python of the CD ⼀ ⼀ a map type. He is enclosed in {} key-value pairs. In dict the key is
    the only one ⼀ in saving time, it is calculated according to a memory address ⼀ key. the key-value is then stored in the address. such an algorithm is called a hash algorithm, therefore, bear in mind that dict key-value stored in the key 'must be hash, and even if you do not understand what is hash, temporarily, so remember, you can not change are the hash, then can hash it means this is immutable for accurate calculation of the memory address and a predetermined ⽽.
    known hashable (non-variable) data types: int, str, tuple, bool not hash (variable) data types: list, dict, set
    syntax:
    {key1: VALUE1, key2: value2 ....}
    Note:.. key must be immutable (hashable) the value does not require any type of data can be stored
    # legitimate
    dic = {123 : 456, True: 999, " id": 1, "name": 'sylar', "age": 18, "stu": [ ' handsome', 'female beauty ⼥'], (1, 2 3): 'twist vine'}
    Print (DIC [123])
    Print (DIC [True])
    Print (DIC [ 'ID'])
    Print (DIC [ 'STU'

    Not illegal

    dic = {[1, 2, 3]: 'Jay'}

    dic = {{1: 2}: "ha"}

    dic = {{1, 2, 3}: 'oh oh'}

    list is variable. not not be used as key # dict is variable. not not be used as key # set is variable, not can not serve as key

    dict saved data is not added to it in accordance with our order to save. is in the order of hash tables to save. ⽽ hash table rather than continuous, so is not supported while trekking slice ⼯ work. It can only be acquired through key dict data
    ⼆ two. CRUD dictionary and other related operations 1. increase

= {} DIC
DIC [ 'name'] = 'Chow' # dict If this key does not appear, it will add a combination of the key-value ⼀ into dict
DIC [ 'Age'] = 18 is
Print (DIC)

If dict did not appear in the key-value. Setdefault can set the default value dic.setdefault ( 'Li Ka-shing') # ⾯ surface can also be set to a value ⾥ ri.

dic.setdefault ( "Li Ka-shing," "real estate") # If the dict already exists. So setdefault will not not play for Use with
print (dic)

  1. 删除
    ret = dic.pop("jay")
    print(ret)
    del dic["jay"]
    print(dic)

    Random deleted.

    K = dic.popitem ()

    Empty all the contents dic.clear dictionary ()

  2. Modified
    DIC = { "ID": 123, "name": 'Sylar', "Age": 18 is}
    DIC1 = { "ID": 456, "name": "twist vine", "ok": "wtf "}
    dic.update (dic1) # dic1 the content is more updated to dic in. If you key the same name. modifying replaced. If the key does not exist, then add.
    Print (dic)
    Print (dic1)
  3. Queries Queries Using ⼀ generally used to find the specific data key.
    Print (DIC [ 'name'])

    print(dic['sylar']) # 报错 print(dic.get("ok"))

print(dic.get("sylar")) # None print(dic.get("sylar", "⽜牛B")) # ⽜牛B

  1. Other related operations
    dic = { "id": 123 , "name": 'sylar', "age": 18, "ok": " Branch ⽐ than"}
    Print (dic.keys ()) # dict_keys ([ 'ID ',' name ',' age ',' ok ']) will not be used is not whether it is even used as a list of what to use it ⾏ use trekking.
    for Key in dic.keys ():
    Print (Key)
    Print (dic .values ()) # dict_values ([ 123, 'sylar', 18, ' Section ⽐ than'] as) use ⼀ also be used when the list.
    for dic.values value in ():
    Print (value)
    Print (DIC. items ()) # dict_items ([ ( 'id', 123), ( 'name', 'sylar'), ( 'age', 18), ( 'ok', ' subjects ⽐ ratio')]) the East ⻄ but not only the West but also list list is loaded with tuple.
    for Key, value in dic.items (): # ?? this is the deconstruction
    print (key, value)

    Deconstruction

    a, b = 1, 2 print(a, b)
    (c, d) = 3, 4
    print(c, d)
    e, f = [1, 2, 3]
    print(e, f)

    Deconstruction when the attention of the amount must match the number of

    Three nested dictionary
    # = {dictionary nested DIC1
    "name": "Feng Wang", "Age": 18 is, "wife": {
    "name": 'Cap Submenu ZZ',
    "Age": 28} ,
    "Children": [ 'first shot a ⽑ wool kids or child', 'second shot two ⽑ wool kids or child'],
    "desc": "peak brother does not tell me it does not matter I want to headline. ' }
    Print (dic1.get ( "wife"). GET ( "name"))

Print (dic1.get ( "Children"))
Print (dic1.get ( "Children") [. 1])
Exercise:
DIC1 = {
'name': [ 'Alex', 2,3,5],
}. 1, the
2
3
4 Oldboy alex deleted.
'Job': 'Teacher',
'Oldboy': { 'Alex': [ 'python1', 'python2', 100]}
name corresponding to a column list element is added ⼀ 'wusir'.
The corresponding column list name in
, oldboy corresponding dictionary plus ⼀ a key-value pairs
, the
corresponding dictionary
alex therefore especially the first word ⺟ mother zoomed capital.
'
⽼ appearing boy
', 'Linux'
.
Corresponding list column python2

Guess you like

Origin www.cnblogs.com/yet-320/p/10992782.html