一、字典(dict)
  1.特性
    1)通过键值对来表示元素
    2)key不能重复且不可修改,如果重复认为是同一个元素
  2.创建

    1)后进先出,若两个key相同,则返回后面进去那个val值
    2)通过键值对创建:dict_1={'key1':'val1','key2':'val2'}

dict_1={'key1':'val1','key2':'val2'}
print(dict_1)

    3)创建:dict_1=dict(key1='val1',key2='val2')

dict_2=dict(key1='val1',key2='val2')
print(dict_2)

  3.映射

list_1=[1,2,3]
list_2=['a','b','c']
res=dict.fromkeys(list_1,list_2)
print(res)      打印出{1: ['a', 'b', 'c'], 2: ['a', 'b', 'c'], 3: ['a', 'b', 'c']}

  4.获取
    1)通过key获取:dict_1['key1']    

dict_1={'key1':'val1','key2':'val2'}
print(dict_1['key1'])

    2)通过get获取:dict_1.get('key3','val3')   若key3不存在,则val3即为key3的值   

dict_1={'key1':'val1','key2':'val2'}
res=dict_1.get('key1')
res1=dict_1.get('key3','val3') key3没有,默认就是后面val3
print(res) 打印出val1
print(res1) 打印出val3

    3)获取key:dict.keys()     获取到的key值的类型为dict_key,无法直接使用,需要list()强制转换

dict_1={'key1':'val1','key2':'val2'}
keys=dict_1.keys()
print(keys) 获取所有的keys值,获取的值类型是dict_key,需要强转
print(list(keys))

    4)获取val:dict.values()   获取到的val值的类型为dict_valuse,无法直接使用,需要list()强制转换

dict_1={'key1':'val1','key2':'val2'}
vals=dict_1.values()
print(vals) 获取所有的val值,获取的值类型是dict_valuse,需要强转
print(list(vals))

    5)获取key和val:dict.items()     获取到的key和val值的类型为dict_items,无法直接使用,需要list()强制转换 

dict_1={'key1':'val1','key2':'val2'}
print(list(dict_1.items()))    打印出[('key1', 'val1'), ('key2', 'val2')]

  5.修改
    1)通过key修改val:dict_1['key1']='8888'   若key存在,则修改key的val值;若key不存在,则自动新增键值对。   

dict_1={'key1':'val1','key2':'val2'}
dict_1['key1']='8888'
print(dict_1)
dict_2={'key1':'val1','key2':'val2'}
dict_2['key3']='8888'
print(dict_2)

    2)dict_1.setdefault(key,val)
      key存在不做操作

dict_3={'key1':'val1','key2':'val2'}
dict_3.setdefault('key1','888')
print(dict_3)

      key不存在新增键值对

dict_4={'key1':'val1','key2':'val2'}
dict_4.setdefault('key3','888')
print(dict_4)

    3)dict_1.update(dict_2)
      key相同,则覆盖
      key不同,则将dict_2追加到dict_1

dict_5={'key1':'val1','key2':'val2'}
dict_6={'key3':'val3','key4':'val4'}
dict_5.update(dict_6)
print(dict_5)

  6.删除
    1)del  ——   del dict_1['key1'] 

dict_7={'key1':'val1','key2':'val2'}
del dict_7['key1']
print(dict_7)

    2)pop  ——  dict_1.pop('key1')  返回被删除的key

dict_8={'key3':'val3','key4':'val4'}
res=dict_8.pop('key3')
print(dict_8)     打印出删除后的dict
print(res)    打印出被删除的key

    3)删除最后一组key:popitem  ——  res2=dict_8.popitem() 返回被删除的key和val

dict_8={'key3':'val3','key4':'val4'}
res2=dict_8.popitem()
print(res2)      打印出被删除的key和val
print(dict_8)    打印出删除后的dict_8
默认删除最后一组key
多操作几次会继续删

    4)清空:clear
      dict_1.clear()

dict_1={'key1':'val1','key2':'val2'}
dict_1.clear()
print(dict_1)

  7.深浅拷贝(只针对容器而言,非容器不考虑深浅拷贝)
    1)浅拷贝
      只拷贝第一层目录,不拷贝第二层内容

      当浅拷贝一份new_dict时,原先dict_1修改后,dict_1显示修改后的,new_dict也会跟着dict_1显示

dict_1={'key1':'val1','key2':'val2','key3':[1,2,3,4]}
new_dict=dict_1.copy()
print('老字典:',dict_1,id(dict_1))             #老字典: {'key1': 'val1', 'key2': 'val2', 'key3': [1, 2, 3, 4]} 2593302979072
print('新字典:',new_dict,id(new_dict))         #新字典: {'key1': 'val1', 'key2': 'val2', 'key3': [1, 2, 3, 4]} 2593298193408

key3=dict_1['key3']
key3.append('test')
print('老字典:',dict_1,id(dict_1))             #老字典: {'key1': 'val1', 'key2': 'val2', 'key3': [1, 2, 3, 4, 'test']} 2593302979072
print('新字典:',new_dict,id(new_dict))         #新字典: {'key1': 'val1', 'key2': 'val2', 'key3': [1, 2, 3, 4, 'test']} 2593298193408

    2)深拷贝
      不仅拷贝第一层目录,还拷贝第二层内容

      当深拷贝一份new_dict时,原先dict_2修改后,dict_2显示修改后的,new_dict仍显示原先深拷贝到的dict_2(可以理解为拷贝到了本地)

import copy
dict_2={'key1':'val1','key2':'val2','key3':[1,2,3,4]}
new_dict2=copy.deepcopy(dict_2)
print('老字典:',dict_2,id(dict_2))         #老字典: {'key1': 'val1', 'key2': 'val2', 'key3': [1, 2, 3, 4]} 2593302979264
print('新字典:',new_dict2,id(new_dict2))   #新字典: {'key1': 'val1', 'key2': 'val2', 'key3': [1, 2, 3, 4]} 2593302979392

key4=dict_2['key3']
key4.append('test')
print('老字典:',dict_2,id(dict_2))         #老字典: {'key1': 'val1', 'key2': 'val2', 'key3': [1, 2, 3, 4, 'test']} 2593302979264
print('新字典:',new_dict2,id(new_dict2))   #新字典: {'key1': 'val1', 'key2': 'val2', 'key3': [1, 2, 3, 4]} 2593302979392

  8.遍历
    1)遍历key:dict.keys()
    2)遍历value:dict.values()
    3)遍历items:dict.items()
  9.排序——加密时候使用

    1)正序排序:sorted(),生成新的list表排序,需要强制转换成dict

dict_1={'key3':'val3','key4':'val4'}
dict_2=sorted(dict_1.items())
print(dict(dict_2))

    2)倒序排序:sorted(reverse=True),生成新的list表排序,需要强制转换成dict

dict_1={'key3':'val3','key4':'val4'}
dict_2=sorted(dict_1.items(),reverse=True)
print(dict(dict_2))

二、集合
  1.特性
    1)无序
    2)元素不可以重复
    3)不支持索引取值
  2.创建:通过{}表示:set1={1,2,3,4}
    
  3.添加元素:add:set1.add()
    
  4.删除
    1)pop:删除第一个

set_1={1,2,3,6,7}
set_1.pop()
print(set_1)

    2)remove:删除指定元素

set_1={1,2,3,6,7}
set_1.remove(6)
print(set_1)

    3)clear:清空

set_1={1,2,3,6,7}
set_1.clear()
print(set_1)

  5.去重:set(),list用set去重,去重后是集合,需要list强转

list_1=[1,2,2,3,4,4,4,4,5,5]
new_list=list(set(list_1))
print(new_list)

三、数据类型转换:关键字(对象)

  1)int与str相互转换

a=100
print(str(a))
a='100'
print(int(a))

  2)list与tuple相互转换

tuple1=(1,2,3) 
print(list(tuple1))
list1=[1,2,3] 
print(tuple(list1))

  3)list与set相互转换

set_1={1,2,3,6,7}
print(list(set_1))
list_1=[1,2,3]
print(set(list_1))

  4)tuple与set相互转换

set_1={1,2,3,6,7}
print(tuple(set_1))
tuple1=(1,2,3)
print(set(tuple1))