类型转换+深浅拷贝

  1. 类型转换

    '''
      “容器”类型: list tuple set dict
      类型: str
      list ---> tuple,set,dict(前提:列表必须符合[(1,''),(2,'')])
      tuple ---> list
      set  ---> list
      dict ---> list  (但是只是把字典中可以保存到列表中)
    
      list()   ---> 转列表
      str()   ---> 转字符串
      tuple()  ----> 转元组
      set()   ----> 转集合
      dict()  ----> 转字典
    '''
    
    list1 = [1, 3, 5, 6, 7, 1, 3, 5, 1, 3, 5]
    # list ---> tuple
    tuple1 = tuple(list1)
    print(tuple1)
    
    # 转成集合类型就可以去除重复元素
    set1 = set(list1)  # 将列表转成set类型
    print(set1)
    
    # list ---》 dict
    # dict1 = dict(list1)
    # print(dict1)
    
    list2 = [(1, 'aa'), (2, 'bb'), (3, 'cc'), (4, 'dd'), (5, 'ee')]
    dict1 = dict(list2)
    print(dict1)
    
    tuple1 = ('1', 'a', 'y', 'o')
    list3 = list(tuple1)
    print(list3)
    
    set2 = {4, 8, 0, 7}
    
    list4 = list(set2)
    print(list4)
    
    dict2 = {1: 'aa', 2: 'bb', 3: 'cc', 4: 'dd', 5: 'ee'}
    list5 = list(dict2)
    print(list5)
    
    s = str(list5)  # '[1, 2, 3, 4, 5]'
    print(s, type(s))
    print('zhangsan')
    
    s1 = str(dict2)  # '{1: 'aa', 2: 'bb', 3: 'cc', 4: 'dd', 5: 'ee'}'
    print(s1, type(s1))
    
    #
    s2 = "['hello,''hi','world']"
    r = list(s2)
    print(r)
    
    s3 = 'hello'
    r = list(s3)
    print(r)
    

     

  2. 浅拷贝

    '''
    可变:  list  set  dict
    不可变: str int float bool tuple
     浅拷贝:
       1.list2 = list1.copy()
       2. import copy
          list3 = copy.copy(list1)
     复制原列表中一模一样的内容到一块新的地址中
    
    
     可变类型共用的是一个地址,无论哪一方对可变类型进行了添加或者删除操作,
        地址都不会发生改变,只不过内容变化
     不可变类型一开始的时候使用的也都是相同的地址,只要不可变类型中有发生改变,哪个列表发生改变
     则地址改变,对另一个列表没有元素地址改变。
    
    
    
    '''
    import copy
    
    list1 = ['aa', 50, ['bb', 'cc', 'dd']]
    # 备份
    list2 = list1.copy()
    # list3 = list1.copy()
    list3 = copy.copy(list1)
    
    print(id(list1), id(list2), id(list3))
    print(list1)
    print(list2)
    print(list3)
    
    for i in list1:
        print(id(i), end='\t')
    
    print('')
    for i in list2:
        print(id(i), end='\t')
    
    print('')
    for i in list3:
        print(id(i), end='\t')
    
    # print()
    # print('*' * 50)
    #
    # list1[0] = 'ee'
    #
    # list2[1] = 60
    #
    # for i in list1:
    #     print(id(i), end='\t')
    #
    # print('')
    # for i in list2:
    #     print(id(i), end='\t')
    #
    # print()
    # print('*' * 50)
    #
    # list1[2].append('ff')
    #
    # for i in list1:
    #     print(id(i), end='\t')
    #
    # print('')
    # for i in list2:
    #     print(id(i), end='\t')
    
    
    # 切片操作是浅拷贝
    # list.copy()
    # copy.copy()
    
    list1 = ['aa', 50, ['bb', 'cc', 'dd']]
    
    list2 = list1  # 赋值操作
    
    list3 = list1[:]
    
    list4 = list1[::-1]
    
    print(list1)
    print(list2)
    print(list3)
    print(list4)
    
    print(id(list1), id(list2),id(list3),id(list4))
    
    print(id(list1[2]),id(list3[2]),id(list4[0]))

     

  3. 深拷贝

    '''
    copy.deepcopy()
    
    两个列表中可变类型会各自使用各自(只要使用深拷贝,可变类型就会新建一份放到备份列表)
    备份列表中的可变类型地址与原列表中的可变类型的地址是不同的。
    深拷贝会完全复制原变量相关的所有数据,在内存中生成一套完全一样的内容,
    在这个过程中我们对这两个变量中的一个进行任意修改,都不会影响其他变量
    
    而浅拷贝: 原列表与备份列表使用的是同一个可变类型的地址
    
    不可变类型与浅拷贝相同
    '''
    
    import copy
    
    list1 = ['aa', 50, ['bb', 'cc', 'dd']]
    
    list2 = copy.deepcopy(list1)
    # 31849352 31883464
    print(id(list1), id(list2))
    print(list1)
    print(list2)
    
    for i in list1:
        print(id(i), end='\t')
    
    print('')
    for i in list2:
        print(id(i), end='\t')
    
    print('')
    
    print('*' * 50)
    list1[2].append('ff')
    print(list1)
    print(list2)
    
    for i in list1:
        print(id(i), end='\t')
    
    print('')
    for i in list2:
        print(id(i), end='\t')
    
    print('')
    
    print('*' * 50)
    
    list1[0] = 'ee'
    list2[1] = 60
    
    print(list1)
    print(list2)
    
    for i in list1:
        print(id(i), end='\t')
    
    print('')
    for i in list2:
        print(id(i), end='\t')
    
    # list2 = list1[:]
    

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值