集合与深浅copy

数据类型的补充:

1,元组:
元组里面如果只有一个元素且没逗号,则该数据的数据类型与里面的元素相同。
tu1 = ([1,2,3])      
tu2 = ([1,2,3],)
print(tu1,type(tu1)    无逗号列表   #[1, 2, 3] 
print(tu2,type(tu2))         有逗号元组 #([1, 2, 3],)
2,list 列表
l1 = [111,222,333,444,555,]
索引为奇数对应的所有元素全部删除。
方法一:
del l1[1::2]
print(l1)
方法二:错误展示
 for index in range(len(l1)):
    print('删除之前的index:%s' % index)
    print('删除之前的l1:%s' % l1)
    if index % 2 == 1:
        del l1[index]
    print('删除之后的index:%s' % index)
    print('删除之后的l1:%s' % l1)
print(l1)
在循环一个列表时,最好不要改变列表的大小,会影响你的最终结果。  
方法三:
倒着循环:
l1 = [111,222,333,444,555,]
for index in range(len(l1)-1,-1,-1):
      if index % 2 == 1:
             del l1 [index]
print(l1)
删除整个列表:
lst = ["倚天屠龙记", "诛仙", "看见", "追风筝的人"]
clear()
lst.clear()
print(lst)
# 有问题
for i in lst:
    lst.remove(i)
print(lst)
for i in lst:# for在这里的意义是记录删除的次数
    del lst[0] # 不正确
print(lst)
lst = ["倚天屠龙记", "诛仙", "看见", "追风筝的人"]
# 正确
for i in range(0, len(lst)):
    lst.pop()
print(lst)
# 把要删除的东西保存在一个新列表中
new_lst = []
for el in lst:
    new_lst.append(el)
# 循环新列表. 删除老列表
for e in new_lst:
    lst.remove(e)
print(lst)
View Code
dict字典:
fromkeys用法:
dic = dict.fromkeys('abc',666)
print(dic)
{ 'a':666, 'b' : 666, 'c' : 666 }
dic = dict.fromkeys([1,2,3],[ ])
dic[1].append(666)
print(dic)
{ 1 : [666], 2 : [666] , 3 : [666] }
View Code
在循环dict中,最好不要改变dict的大小,会影响结果或者报错。
dic = {'k1':'v1','k2':'v2','k3':'v3','name':'alex'}
for i in dic:
     if 'k' in i:
         del dic[i]   # (报错)
View Code
  dic = {'k1':'v1' , 'k2':'v2' , 'k3':'v3' , 'name':'alex'}
l1 = [ ]
for key in dic:
      if 'k' in key:
         l1.append(key)
# print(l1)   ['k1' , 'k2' , 'k3' ]
for key in l1:
      del dic[key]
print(dic)      #{'name' : 'alex'}
View Code

把第一个参数中的每一项拿出来和第二个参数组成一个键值对
 生成出来的键指向的value是同一个对象. 改变其中一个的时候. 另一个也会跟着改变
dic = dict.fromkeys(["jay", "jj"], ["周杰伦", "林俊杰"])
dic['jay'].append("蔡依林")
print(dic)
打印出来的是空:
dic = {}
dic.fromkeys(["葫芦娃", "蛇精"], ["冯提莫", "陈一发"])
print(dic) # 什么都不打印.

dic = {"韩哥":"发图不留种", "奎峰":"斗图小皇帝", "徐阳":"开车不留情"}
for item in dic: # #直接循环迭代的时候拿到的是key
    if item == '奎峰':
        dic.pop(item)   # dictionary changed size during iteration
View Code
循环删除的案例
dic = {"韩哥":"发图不留种", "奎峰":"斗图小皇帝", "徐阳":"开车不留情"}
lst = ['奎峰']
for el in lst:  # 循环list
    dic.pop(el) # 删除的是dict中的元素
print(dic)

lst = ["发图哥", "谁困. 下午就往他那发图", "轰炸"]
print(type(lst)) # list
tu1 = tuple(lst)    # tuple
print(tu1)  # ()
print(type(tu1)) # tuple
lst1 = list(tu1)    # list
print(lst1) # []

dic = {"a":'老A', 'b':"王宝强", 'c':"蓉姐", 'd':'百合姐'}

for k in dic:
    if k == 'b' or k == 'c':
        dic.pop(k)  # dictionary changed size during iteration
View Code
删除带有k的元素
dic中的元素在迭代过程中是不允许进行删除的
dic = {'k1':'alex','k2':'wusir','s1':'金老板'}
删除key中带有'k'的元素
for k in dic:
    if 'k' in k:
        del dic[k]
print(dic)  #报错,在循环迭代的时候不允许进行删除操作

dic = {'k1':'alex','k2':'wusir','s1':'金老板'}
dic_1 = []
# 删除带有k的元素
for k in dic:
    if 'k' in k:
        dic_1.append(k)
for el in dic_1:
    del dic[el]
print(dic)
View Code
数据类型的转换:
str-------> list       split 分割
list-------> str     join
tuple----->list     (元组可互换)
tu1 = (1,2,3)
l1 = list(tu1)
print(l1)              #[1, 2, 3] 
tu2 = tuple(l1)
print(tu2)      #(1, 2, 3)
dic------>list    list(dic)(列表中的元素只有key)
dic = {'k1':'v1' , 'k2':'v2' , 'k3':'v3' , 'name':'alex'}
l1 = list(dic)
print(l1)           #['k1', 'k2', 'k3', 'name']
print(list(dic.keys()))       #['k1', 'k2', 'k3', 'name']
print(list(dic.values()))       #['v1', 'v2', 'v3', 'alex']
print(list(dic.items()))    #[('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3'), ('name', 'alex')]
0, " " , [ ] , ( ) , { } ,set() ------>bool  都是Fasle
集合;
基础数据类型  set.
集合是一个可变的数据类型,他是以{ }形式存在的,空集合set( ),但是它要求它里面的元素是不可变的(数字,字符串,元组,bool),集合是无序的,不重复的。
作用:
1,去重
2,关系测试。
列表的去重:set
l1 = [11,11, 22 , 22 , 33 , 33 , 33 , 44]
l2 = list(set(l1))
l2.sort()    #[33, 11, 44, 22]   sort  排序
print(l2)        #[11, 22, 33, 44]
View Code
列表的关系测试:
交集: &, intersetion
两种表示:
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
set3 = set1 & set2
print(set3)      
或者print(set1.intersection(set2))   #{4,5}
View Code
并集:|  union 
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
print(set1 | set2)
print(set1.union(set2))
#{1,2,3,4,5,6,7,8}
#{1,2,3,4,5,6,7,8}
View Code
差集  -  difference
两种表示方法;
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
print(set1 - set2)      set1独有       #{1,2,3}
print(set2.difference(set1))        set2独有   {8,6,7}
View Code
反交集  ^ symmetric_difference
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
两种表示方法:
print(set1 ^ set2)   #{1,2,3,6,7,8}
print(set1.symmetric_difference(set2))
View Code
子集(小于,结果是Ture或Flase) 
set1 = {1,2,3}
set2 = {1,2,3,4,5,6}
print (set1 < set2)    # Ture         set1 是 set2的子集
print(set1.issubset(set2))    #   set1 是 set2的子集
View Code
超级(大于,结果是Ture或Flase)
set1 = {1,2,3}
set2 = {1,2,3,4,5,6}
print(set2 > set1)   #  Ture set2 是 set1的超集
print(set2.issuperset(set1))     # Ture set2 是 set1的超集
View Code
冻集合:
set1 = frozenset({1,2,3,'alex'})
print(set 1, type(set1))
#  frozenset({1,2,3,'alex'}) <class 'frozenst'>
View Code
赋值运算:
l1 = [1,2,3]
l2 = l1
l2.append(666)
print(l1,l2)        #[1, 2, 3, 666] [1, 2, 3, 666]
View Code
对于赋值运算来说,指向的都是同一个内存地址,一直都不变。
深浅copy:
浅copy:
l1 = [11,22,33]
l2 = l1.copy()
l1.append(666)
print(l1,id(l1))        #[11, 22, 33, 666] 31203144
print(l2,id(l2))        #[11, 22, 33] 31203272
l1 = [11, 22, ['barry', [55, 66]], [11, 22]]
l2 = l1.copy( )
l1[2].append('alex')
#print(l1,id(l1))          #11, 22, ['barry', [55, 66], 'alex'], [11, 22]] 41723272
#print(l2,id(l2))   #  [11, 22, ['barry', [55, 66], 'alex'], [11, 22]] 41723336
print(l1, id(l1[-1]))
print(12, id (l2[-1]))
View Code
对于浅copy来说,第一层创建的是新的内存地址,而从第二层开始,指向的都是同一个内存地址,所以,对于第二层以及更深的层数来说,保持一致性。
深copy:完全独立
import copy
l1 = [11,22, ['barry']]
l2 = copy.deepcopy(l1)
l1[2].append('alex')
print(l1,id(l1[-1]))   # [11,22, ['barry' , 'alex' ]] 42282312
print(l2, id(l2[-1]))  # [11,22, ['barry'] ] 42332680
View Code
对于切片来说,是浅copy.
l1 = [1,2,3,4,5,6,[11,22]]
l2 = l1[ : ]
#l1.append(666)
#print(l1,l2)
[1, 2, 3, 4, 5, 6, [11, 22], 666] [1, 2, 3, 4, 5, 6, [11, 22]]
l1[-1].append(666)
print(l1,l2)
[1, 2, 3, 4, 5, 6, [11, 22, 666]] [1, 2, 3, 4, 5, 6, [11, 22, 666]]
View Code
集合的增删查:(集合是无序的)
增:增加无序
1,add:
set1 = {' alex' ,'WuSir', 'RiTiAn' , 'egon' ,' barry'}
set1.add('太白')
print(set1)  # {'alex', '太白‘ , ’barry' ,' Ritian ' , 'Wusir' , 'egon' }
View Code
2,update:迭代着增加
set1 = {' alex' ,'WuSir', 'RiTiAn' , 'egon' ,' barry'}
set1.update('abc')
set1.update([111, 2222, 333])
print(set1)
{'egon', 'a', 'c', ' barry', 333, 2222, 111, ' alex', 'WuSir', 'RiTiAn', 'b'}
View Code
删:无序
1,remove 按元素删除
set1 = {' alex' ,'WuSir', 'RiTiAn' , 'egon' ,' barry'}
set1.remove('RiTiAn')
print(set1)
View Code
2,pop随机删除,有返回值
set1 = {' alex' ,'WuSir', 'RiTiAn' , 'egon' ,' barry'}
print(set1.pop())
print(set1)
View Code
3,clear清空
set1 = {' alex' ,'WuSir', 'RiTiAn' , 'egon' ,' barry'}
set1.clear()
print(set1)      # set()
View Code
4,del删除整体
set1 = {' alex' ,'WuSir', 'RiTiAn' , 'egon' ,' barry'}
del set1
print(set1)
View Code
查:
set1 = {' alex' ,'WuSir', 'RiTiAn' , 'egon' ,' barry'}
for i in set1:
       print(i)
##  egon
      RiTiAn
      WuSir
      barry
      alex

二者区别:
print(set1)
# {'RiTiAn', ' barry', 'egon', 'WuSir', ' alex'}
   {'RiTiAn', ' barry', 'egon', 'WuSir', ' alex'}
   {'RiTiAn', ' barry', 'egon', 'WuSir', ' alex'}
   {'RiTiAn', ' barry', 'egon', 'WuSir', ' alex'}
   {'RiTiAn', ' barry', 'egon', 'WuSir', ' alex'}
View Code

 导入copy模块

import copy
lst1 = ["杨晓", "谢逊", ["说不得","听不得","打不得","骂不得"]]
lst2 = copy.deepcopy(lst1) # 深度拷贝
print(id(lst1[2]), id(lst2[2]))
lst1[2].append("看不得")
print(lst1, lst2)
View Code

 

 

 

 

posted on 2018-05-27 14:46  liangliang123456  阅读(164)  评论(0编辑  收藏  举报

导航