• 7,数据类型转换,set 集合,和深浅copy


    str转换成list  用split

    list转换成str  用join

    tuple转换成list

    tu1 = (1,2,3)

    li = list(tu1)(强转)

     tu2 = tuple(li)(强转)

    dic转换成list

    c2 = list(dic)

    各种数据类型转换成bool

    0,'',[],(),{}, set() 转成bool都是Fasle

    在循环列表或者字典删除元素的过程中,有可能会影响结果,有可能会报错。

    报错信息如下:

    RuntimeError: dictionary changed size during iteration

    解释:运行时错误:字典在迭代期间更改了大小。

    集合

    set2 = {11,11,11,44}  set去重
    print(set2)

    l1 = [11, 11, 22, 22, 33, 33, 33, 44]    列表去重
    l2 = list(set(l1))
    l2.sort()
    print(l2)

    增(add,update)

    set1 = {'alex', 'WuSir', 'RiTiAn', 'egon', 'barry'}按元素增加(不能切片,不能索引,不能不长)
    set1.add('zhangyajie')
    print(set1)
    set1 = {'alex', 'WuSir', 'RiTiAn', 'egon', 'barry'} 迭代添加

    set1.update(['asd'])
    print(set1)

    删(pop,remove,clear,del)

    set1 = {'alex', 'WuSir', 'RiTiAn', 'egon', 'barry'}
    print(set1.pop()) #随机删除
    set1.remove('alex') #按元素删除
    print(set1)

    set1.clear() #清空列表
    print(set1)

    del set1 #删除列表
    print(set1)

    查(for)

    set1 = {'alex', 'WuSir', 'RiTiAn', 'egon', 'barry'}
    li = []
    for i in set1:
        if i.startswith('a') or i.endswith('x'):
            li.append(i)
            print(li)

    交集(intersetion  和 &)

    set1 = {1, 2, 3, 4, 5}
    set2 = {4, 5, 6, 7, 8}
    set3 = set1 & set2
    print(set3)
    print(set1.intersection(set2))

    并集 |  和 union

    print(set1 | set2)
    print(set1.union(set2))

    差集 difference -

    set1 = {1, 2, 3, 4, 5}
    set2 = {4, 5, 6, 7, 8}
    print(set1.difference(set2)) # set1 独有
    print(set2.difference(set1)) # set2 独有

    print(set1 - set2) #set1独有的
    print(set2 - set1) #set2独有的
     
    反差集   ^  和  symmetric_difference
    print(set1 ^ set2)
    print(set1.symmetric_difference(set2))

    子集

    set1 = {1,2,3}
    set2 = {1,2,3,4,5,6}
    print(set1 > set2) # set1是set2的子集

    超集

    set1 = {1, 2, 3}
    set2 = {1, 2, 3, 4, 5, 6}
    print(set2.issuperset(set1))

    冻结 set

    set1 = frozenset({1,2,3, 'alex'})
    print(set1,type(set1))

    赋值运算的内存地址比较:

    对于赋值运算来说,指向的都是同一个内存地址,一变都变。

    l1 = [1, 2, 3]
    l2 = l1
    l3 = l2
    l3.append(666)
    print(id(l1))
    print(id(l2))
    print(id(l3))
    print(l3)
    print(l2)
    print(l1)

    深浅copy

    浅copy

    对于浅copy来说,第一层内存地址是创建新的内存地址(意思就是两个内存地址不一样),第二层开始,共用同一个内存地址,
    所以对于第3,4,5,6曾之后,共用的都是同一个内存地址。
    l1 = [11,22,33] l2 = l1.copy() l1.append(666) print(id(l1)) print(id(l2)) print(l1) print(l2)

    li = [11, 22, ['barry',[222]]]
    l1 = li.copy()
    print(id(li))
    print(id(l1))
    print(id(li[-1]))
    print(id(l1[-1]))

     深copy

    l1 = [11, 22, 33]
    l2 = copy.deepcopy(l1)
    l1.append(666)
    print(l1, id(l1))
    print(l2, id(l2))
    结果:
    [11, 22, 33, 666] 233294746056
    [11, 22, 33] 233294657800


    深copy,是新建了完全独立的内存空间。
    import copy
    l1 = [11, 22, ['barry']]
    l2 = copy.deepcopy(l1)
    l1[2].append('alex')
    print(l1,id(l1))
    print(l2,id(l2))
    print(l1, id(l1[-1]))
    print(l2, id(l2[-1]))
  • 相关阅读:
    iOS基础知识----数据解析
    iOS 向下取整、向上取整、四舍五入
    SourceTree推送时,增加额外的远程仓库,不用每次都自定义粘贴复制网络
    Container View 使用小技巧
    CocoaPods 升级
    新版百度云如何加速
    环信透传消息,无法回调
    Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibr
    Mac Sublime Text complie python .py error /bin/bash: shell_session_update: command not found
    ios 消息转发初探
  • 原文地址:https://www.cnblogs.com/ZJGG/p/9003111.html
Copyright © 2020-2023  润新知