• 8.5(列表,元组,字典,集合的内置方法)


    复习

    数字类型内置方法

    整型,浮点型

    作用:年龄,id 薪资等

    使用方法:算术运算,比较运算

    没有有序无序这一说

    存一个值

    不可变

    字符串类型内置方法

    作用:描述年龄性别

    定义方法:

    ''   ''''   
    加字符
    r
    

    内置方法:
    优先掌握:索引取值 切片 strip split 长度 成员运算 for循环

    需要掌握:upper/lower startswith/endswith rstrip/lstrip rsplit isdigit/isalpha join replace

    了解:find/rfind/index/rindex/count capitalize/swapcase/title center/ljust/rjust/zfill is系列

    存一个值

    有序

    不可变
    有序和无序:有索引就是有序,无索引就是无序

    可变和不可变:值变id不变就是可变,值变id变就是不可变


    列表类型内置方法

    作用:描述多个值

    定义方式:[]内添加,多个用逗号隔开

    内置方法:

    # # 1.索引取值
    # name_list = ['nick','jason','tank','sean']
    # name_list[0] = 'nick handsome'
    # print(f"name_list[0]:{name_list[0]}")
    # #2.切片
    # name_list = ['nick','jason','tank','sean']
    # print(f"name_list[0:3:2]:{name_list[0:3:2]}")
    # # 3.长度
    # name_list = ['nick','jason','tank','sean']
    # print(f"len(name_list):{len(name_list)}")
    # 4.成员运算in或not in
    # name_list = ['nick','jason','tank','sean']
    # print(f"'tank sb' in name_list:{'tank sb' in name_list}")
    # print(f"'nick handsome' not in name_list:{'nick handsome' not in name_list}")
    # # 5.追加值append
    # name_list = ['nick','jason','tank','seam']
    # name_list.append('tank sb')
    # print(f"name_list:{name_list}")
    # # 6.删除del
    # name_list = ['nick','jason','tank','sean']
    # del name_list[2]
    # print(f"name_list:{name_list}")
    # # 7.for循环
    # name_list = ['nick','jason','tank','sean']
    # for name in name_list:
    #     print(name)
    # 需要掌握
    # 1.insert(插入)
    # name_list = ['nick','jason','tank','sean']
    # name_list.insert(1,'handsome')
    # print(f"name_list:{name_list}")
    # # 2.pop(删除,默认删除最后一个)
    # name_list = ['nick','jason','tank','sean']
    # print(f"name_list.pop(1):{name_list.pop(1)}")
    # print(f"name_list:{name_list}")
    # # 3.remove移除
    # name_list = ['nick','jason','tank','sean']
    # print(f"name_list.remove('nick):{name_list.remove('nick')}")
    # print(f"name_list:{name_list}")计数
    # 4.count计数
    # name_list = ['nick','jason','tank','sean']
    # print(f"name_list.count('nick'):{name_list.count('nick')}")
    # # 5.index索引
    # name_list = ['nick','jason','tank','sean']
    # print(f"name_list.index('nick):{name_list.index('nick')}")
    # # 6.clear清除
    # name_list = ['nick','jason','tank','sean']
    # name_list.clear()
    # print(f"name_list:{name_list}")
    # # 7.copy复制
    # name_list = ['nick','jason','tank','sean']
    # print(f"name_list,copy():{name_list.copy()}")
    # 8.extend拓展
    # name_list = ['nick','jason','tank','sean']
    # name_list2 = ['nick handsome']
    # name_list.extend(name_list2)
    # print(f"name_list:{name_list}")
    # # 9.reverse翻转
    # name_list = ['nick','jason','tank','sean']
    # name_list.reverse()
    # print(f"name_list:{name_list}")
    # # 10.sort
    # name_list = ['nick','jason','tank','sean']
    # name_list.sort()
    # print(f"name_list:{name_list}")
    # name_list.sort(reverse=True)
    
    

    一次存多个值

    有序

    可变

    元组类型内置方法

    定义方式:列表中的中括号换为小括号

    作用:与列表相同

    内置方法:

    # # 1.索引取值
    # # name_tuple = ('nick','jason','tank','sean')
    # # print(f"name_tuple[0]:{name_tuple[0]}")
    # # 2.切片
    # name_tuple = ('nick','jason','tank','sean')
    # print(f"name_tuple[1,2,3]:{name_tuple[1:3:2]}")
    # # 3.长度
    # name_tuple = ('nick','jason','tank','sean')
    # print(f"len(name_tuple):{len(name_tuple)}")
    # # 4.成员运算
    # name_tuple = ('nick','jason','tank','sean')
    # print(f"'nick' in name_tuple:{'nick' in name_tuple}")
    # # 5.循环
    # name_tuple = ('nick','jason','tank','sean')
    # for name in name_tuple:
    #     print(name)
    # #6.count()计数
    # name_tuple = ('nick','jason','tank','sean')
    # print(f"name_tuple.count('nick'):{name_tuple.count('nick')}")
    # # 7.index
    # name_tuple = ('nick','jason','tank','sean')
    
    

    一次存多个值

    有序

    没有可变不可变这一说

    元组与列表的不同

    元组无法修改,元组在定义的时候元素个数与值就固定了,所以没有可变不可变这一说,早起用来减少内存占用,现在毫无用处

    字典类型内置方法

    作用:为值添加描述信息并使用

    定义方式:{}内以逗号隔开加入键值对

    内置方法:

    # # 1.索引取值
    # # name_tuple = ('nick','jason','tank','sean')
    # # print(f"name_tuple[0]:{name_tuple[0]}")
    # # 2.切片
    # name_tuple = ('nick','jason','tank','sean')
    # print(f"name_tuple[1,2,3]:{name_tuple[1:3:2]}")
    # # 3.长度
    # name_tuple = ('nick','jason','tank','sean')
    # print(f"len(name_tuple):{len(name_tuple)}")
    # # 4.成员运算
    # name_tuple = ('nick','jason','tank','sean')
    # print(f"'nick' in name_tuple:{'nick' in name_tuple}")
    # # 5.循环
    # name_tuple = ('nick','jason','tank','sean')
    # for name in name_tuple:
    #     print(name)
    # #6.count()计数
    # name_tuple = ('nick','jason','tank','sean')
    # print(f"name_tuple.count('nick'):{name_tuple.count('nick')}")
    # # 7.index
    # name_tuple = ('nick','jason','tank','sean')
    # print(f"name_tuple.index('nick'):{name_tuple.index('nick')}")
    
    # 1.按key取值,可存可取
    # dic = {'a':1,'b':2}
    # print(f"first dic['a']:{dic['a']}")
    # dic['a'] = 3
    # print(f"second dic['a]:{dic['a']}")
    # # 2.长度len
    # dic = {'a':1,'b':2}
    # print(f"len(dic):{len(dic)}")
    # # 3.in   not in
    # dic = {'a':1,'b':2}
    # print(f"'a' in dic:{'a' in dic}")
    # print(f"l in dic:{1 in dic}")
    # # 4.删除
    # dic = {'a':1,'b':2}
    # del dic['a']
    # print(f"dic.get('a'):{dic.get('a')}")
    #
    # dic = {'a':1,'b':2}
    # dic.pop('a')
    # print(f"dic.pop('b'):{dic.pop('b')}")
    # print(f"dic.get('a'):{dic.get('a')}")
    #
    # dic = {'a':1,'b':2}
    # print(f"dic.popitem():{dic.popitem()}")
    # 5.键keys   值values   键值对items
    # dic = {'a':1,'b':2}
    # print(f"dic.keys():{dic.keys()}")
    # print(f"dic.values():{dic.values()}")
    # print(f"dic.items():{dic.items()}")
    #
    # # 6.循环
    # dic = {'a':1,'b':2,'c':3,'d':4}
    # for k, v in dic.items():
    #     print(k,v)
    #
    # # 1.get
    # dic = {'a':1,'b':2}
    # print(f"dic.get('a'):{dic.get('a')}")
    # print(f"dic.get('c'):{dic.get('c')}")
    # # 2.update
    # dic1 = {'a':1,'b':2}
    # dic2 = {'c':3}
    # dic1.update(dic2)
    # print(f"dic1:{dic1}")
    # 3.formkeys
    # dic = dict.fromkeys(['name','age','sex'],None)
    # print(f"dic:{dic}")
    # # 4.setdefault
    # dic = {'a':1,'b':2}
    # print(f"dic.setdefault('a'):{dic.setdefault('a',3)}")
    # print(f"dic:{dic}")
    # print(f"dic.setdefault('c'):{dic.setdefault('c',3)}")
    # print(f"dic:{dic}")
    

    一次存多个值

    无序

    可变

    集合类型内置方法

    作用:储存一大堆元素的东西,容器数据类型

    定义方式:以{}隔开数据 去重,乱序

    内置方法:

    # 1.长度len
    # s = {1,2,'a'}
    # print(f"len(s):{len(s)}")
    # # 2.in  not in
    # s = {1,2,'a'}
    # print(f"1 in s:{1 in s}")
    # # 3.|并集
    # pythoners = {'jason','nick','tank','sean'}
    # linuxers = {'nick','egon','kevin'}
    # print(f"pythoners|linuxers:{pythoners|linuxers}")
    # print(f"pythoners.union(linuxers):{pythoners.union(linuxers)}")
    # # 4.&交集
    # pythoners = {'jason','nick','tank','sean'}
    # linuxers = {'nick','egon','kevin'}
    # print(f"pythoners&linuxers:{pythoners&linuxers}")
    # print(f"pythoners.intersection(linuxers):{pythoners.intersection(linuxers)}")
    # 5.-差集
    # pythoners = {'jason','nick','tank','sean'}
    # linuxers = {'nick','egon','kevin'}
    # print(f"pythoners-linuxers:{pythoners-linuxers}")
    # print(f"pythoners.difference(linuxers):{pythoners.difference(linuxers)}")
    # # 6.对称差集
    # pythoners = {'jason','nick','tank','sean'}
    # linuxers = {'nick','egon','kevin'}
    # print(f"pythoners^linuxers:{pythoners^linuxers}")
    # print(f"pythoners.symmetric_difference(linuxers):{pythoners.symmetric_difference(linuxers)}")
    # 7.==
    # pythoners = {'jason','nick','tank','sean'}
    # linuxers = {'nick','egon','kevin'}
    # javers = {'nick','egon','kevin'}
    # print(f"pythoners==linuxers:{pythoners==linuxers}")
    # print(f"javers==linuxers:{javers==linuxers}")
    # # 8.父集
    # pythoners = {'jason','nick','tank','seam'}
    # linuxers = {'nick','egon','kevin'}
    # javaers = {'jason','nick'}
    # print(f"pythoners>linuxers:{pythoners>linuxers}")
    # print(f"pythoners>=linuxers:{pythoners>=linuxers}")
    # print(f"pythoners>=javaers:{pythoners>=javaers}")
    # print(f"pythoners.issuperest(javaers):{pythoners.issuperset(javaers)}")
    # # 9.子集
    # # 1.add
    # s = {1,2,'a'}
    # s.add(3)
    # print(s)
    # # 2.remove
    # s = {1,2,'a'}
    # s.remove(1)
    # print(s)
    # 3.difference_update
    # pythoners = {'jason','nick','tank','sean'}
    # linuxers = {'nick','egon','kevin'}
    # pythoners.difference_update(linuxers)
    # print(f"pythoners,difference_update(linuxers):{pythoners}")
    # # 4.discard
    # s = {1,2,'a'}
    # s.discard(3)
    # print(s)
    # # 5.isdisjoint
    # pythoners = {'jason','nick','tank','sean'}
    # linuxers = {'nick','egon','kevin'}
    # pythoners.isdisjoint(linuxers)
    
    

    存多个值

    无序

    可变

  • 相关阅读:
    ES6中对象新增的方法
    ES6中数组新增的方法超级好用
    让你轻松看懂defer和async
    es6新增的运算符链判断运算符的诞生[?.]和null的判断运算符??
    学习React中的jsx保证你看的明明白白
    js中forEach的用法、forEach如何跳出循环、forEach与for之间的区别
    EM算法学习笔记
    macOS使用Homebrew安装包失败,报错:tar: Error opening archive: Failed to open
    深度网络重参数化
    逻辑回归学习笔记
  • 原文地址:https://www.cnblogs.com/jiann/p/11305390.html
Copyright © 2020-2023  润新知