• day7 集合


    day7 集合
    集合:可变的数据类型,里面的元素必须是不可变的数据类型
    无序的,不重复
    {}
    set1 = set({1,2,3})
    set2={1,2,3,[2,3],{'name':'alex'}}#集合里面的元素必须是不可变的数据类型
    print(set1)
    print(set2)
    set1 = {'alex','wusir','ritian','egon','barry'}
    #add
    set1.add('女神')
    print(set1)
    set1.update('abc')
    print(set1)
    #删除
    set1.pop()#随机删除
    print(set1.pop()) #有返回值
    print(set1)

    set1.remove('alex') #按元素删除
    print(set1)

    #清空集合 空集合:set()
    set1.clear()
    print(set1)

    #删除整个集合
    del set1
    print(set1)

    #交集
    set1 = {1,2,3,4,5}
    set2 = {4,5,6,7,8}
    set3 = set1 & set2 #{4,5}
    print(set1.intersection(set2)) #{4,5}
    print(set3)
    #并集
    set1 = {1,2,3,4,5}
    set2 = {4,5,6,7,8}
    print(set1 | set2)
    print(set2.union(set1))

    #反交集
    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)) # {1, 2, 3, 6, 7, 8}

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

    #子集
    set1 = {1,2,3,}
    set2 = {1,2,3,4,5,6}
    print(set1 < set2)
    print(set1.issubset(set2)) # 这两个相同,都是说明set1是set2子集。

    #超级
    print(set2 > set1)
    print(set2.issuperset(set1)) # 这两个相同,都是说明set2是set1超集。

    #面试题:列表去重
    li=[1,2,33,33,2,1,4,5,6,6]
    set1 = set(li)
    print(set1)
    li = list(set1)
    print(li)

    # frozenset不可变集合,让集合变得不可变
    s = frozenset('barry')
    print(s,type(s))
    for i in s:
    print(i)

    import copy
    l1 = [1,2,[4,5,6],3]
    l2 = copy.deepcopy(l1)
    print(l1,id(l1))
    print(l2,id(l2))
    l1[2].append('a')
    print(l1,l2)

    l1 = [1,[1],2,3,4]
    l2 = l1[:]
    l1[1].append('a')
    l2 的结果是什么?


    print(l1,id(l1))
    print(l2,id(l2))
    print(l1[1] is l2[1])

    li = ['alex','taibai','wusir','egon']
    for i in li:
    print(li.index(i),i)

    for index,i in enumerate(li,1):
    print(index,i)

    #循环中不能删除列表中元素,删除后索引后变
    lis = [11,22,33,44,55]
    # for i in range(len(lis)):
    # print(i)
    # del lis[i]
    # print(lis)

    基础数据类型汇总
    str
    # s = ''
    # print(s.isspace())
    int
    '''
    list:
    '''
    lis = [11,22,33,44,55]
    for i in range(len(lis)):
    print(i) # i = 0 i = 1 i = 2
    del lis[i]
    print(lis) # [11,22,33,44,55] [22, 44, 55] [22, 44]

    #第一种
    lis = lis[::2]
    print(lis)

    #第二种
    l1 = []
    for i in lis:
    if lis.index(i) % 2 == 0:
    l1.append(i)
    lis = l1
    print(lis)

    lis = [11,22,33,44,55]
    # for i in range(len(lis)-1,-1,-1):
    # if i % 2 == 1:
    # print(i)
    # del lis[i]
    # print(lis)
    # print(lis)

    dic = dict.fromkeys([1,2,3],'春哥')
    print(dic)
    dic = dict.fromkeys([1,2,3],[])
    print(dic) # {1: [], 2: [], 3: []}
    dic[1].append('袁姐')
    print(dic)
    dic[2].extend('二哥')
    print(dic)


    l1 = []
    l2 = l1
    l3 = l1
    l3.append('a')
    print(l1,l2,l3)

    dic = {'k1':'v1','k2':'v2','a3':'v3'}
    dic1 = {}

    for i in dic:
    if 'k' not in i:
    dic1.setdefault(i,dic[i])
    dic = dic1
    print(dic)
    l = []
    for i in dic:
    if 'k' in i:
    l.append(i)
    for i in l:
    del dic[i]
    print(dic)

    转化成bool值
    0 '' [] () {} set()

    #元祖 如果元祖里面只有一个元素且不加,那此元素是什么类型,就是什么类型。
    tu1 = (1)
    tu2 = (1,)
    print(tu1,type(tu1))
    print(tu2,type(tu2))
    tu1 = ([1])
    tu2 = ([1],)
    print(tu1,type(tu1))
    print(tu2,type(tu2))
    dic = dict.fromkeys([1,2,3,],3)
    dic[1] = 4

  • 相关阅读:
    Atitit  atiMail atiDns新特性 v2  q39
    Atitit.java jar hell解决方案Djava.ext.dirs in ide envi..
    砍价大法
    DWR学习
    什么是WEB 2.0
    XHTML 1.0 Tags 参考
    web.config文件遇到的错误
    JDBC教程之PreparedStatement
    符合W3C标准的target=_blank形式
    dwr 登录实现 (入门知识)
  • 原文地址:https://www.cnblogs.com/Murraya/p/10504374.html
Copyright © 2020-2023  润新知