Python Basics (five) ------ dictionary

Python Basics (d) ------ dictionary

dictionary

What is a dictionary Dian

dict keywords to {} represent for key: value pairs stored data, each separated by commas

Key: must be a hash, (immutable data type) must be unique

Found: any data type

Features:

Query efficiency is very high, to find elements by key

Internal key to calculate a memory address (temporarily), hash algorithm, key data must be immutable type (hash key must be data type), key data must be immutable type

A dictionary is unordered, python3.6 version of the above, the definition of default order of 3.5 or less randomly display

Use two Dian dictionary

####增
    #setdefault('键名','值') 
    #两个含义:
        #1.如果key不存在就执行新增.
        #2.如果key已经存在,就不在新增,就执行查询 
    dic.setdefault('键名','值')
    
    #添加键值
    dic['键名']='值'

    
    

####删
    #pop 通过键,删除,返回被删除的键锁对应的值
    dic.pop('键名') 

    #popitem() 随机删除
    dic.popitem()

    #del dic[键名] #指定的键值对
    del dic['键名']

    #del 删除整个字典
    del dic   # ---> 删除的是内存地址
    
    #clear() 清空字典
    dic.clear()

    
    
    
####改
    #直接修改   
    dic['key']='新值'
    
    #update() 替换修改
    dic.update({'键名':'新值'}) #括号内的字典优先级高

    
    
    
####查
    #直接查询
    dic('键')

    #setdefault('键名') 前提是键已存在的情况,执行查询, 
    dic.setdefault('键名')

    #get('键名','返回的内容')键存在,返回对应的值. 键不存在, 默认返回的None,若指定提示内容,则返回提示内容
    dic.get('键名','键不存在,返回的内容')

    
    

#####字典的使用
    #for 循环字典 ,获取所有的键
    for i in dic:
        print(i)
               
    #dic.keys() 
    # 获取所有的keys  高仿列表,但是没有索引,可迭代.可以根据keys获取值
    a={'a':'1',"b":'2'}
    print(a.keys())
    
    for i in dic.keys():
        print(dic[i])
        
    #dic.values()
    # 获取所有的values,没有索引,可迭代
    a={'a':'1',"b":'2'}
    print(a.values())
    
    for i in dic.values():
        print(i)   
        
    #dic.items() 
    #获取字典汇中的键值对,以元组形式.
    a={'a':'1',"b":'2'}
    print(a.items()) # dict_items([('a', '1'), ('b', '2')])
    
    for i in dic.items()
        print(i)   #-->('a', '1')
        print(type(i)) #<class 'tuple'>
    
    
    
    
####解构  
    #字典  元组   列表  ,字符串 可迭代的都能解构
    a,b,c=(a1,b1,c1) # 必须一一对应
    a,b={    #   解构的是字典的key
        "a":1,
        "b":2,
    }
    
    #字典的结构   
    a={'a':'1',"b":'2'}
    for k,v  in a.itmes():
        print(k,v)  # --->k 对应键, v 对应值
    
    
    
    
####枚举 enumerate(可迭代对象,索引) 同时列出数据和数据下标
    a={'a':'1',"b":'2'} 
    for i,k enumerate(a,1) #第二个参数,默认从0开始
        print(i,k)  # --- i 是索引,  k是键名

Wed and nesting dictionary

goods = [{"name": "电脑", "price": 1999},
         {"name": "鼠标", "price": 10},
         {"name": "游艇", "price": 20},
         {"name": "美女", "price": 998},
         ]

#取出美女
print(good[3]['name'])

The role of four Dian dictionary

1. The dictionary is to store large amounts of data, larger than the list of dictionary

2. Find the value when the dictionary, convenient, fast

3. The data dictionary can associate

4. The memory consumption of a large dictionary, common data types: strings, lists, and dictionaries

### dictionaries conversion needs the json module

Guess you like

Origin www.cnblogs.com/dengl/p/10997505.html