• 各种数据类型内置方法


    列表类型内置方法

    1.用途: 多个装备,多个爱好,多门课程,甚至多个女朋友.

    2.定义: []内可以有多个任意的值,逗号分隔元素

    my_girl_friend = ['xing', 'kang', 'wang']
    print(my_girl_friend)
    

    ['xing', 'kang', 'wang']

    q = list('hello kang')
    print(q)
    

    ['h', 'e', 'l', 'l', 'o', ' ', 'k', 'a', 'n', 'g']

    3.常用操作+内置方法

    **优先掌握 **

    1. 按索引取值(正向取值+反向取值),即可存也可取
    2. 切片
    3. 长度len
    4. 成员运算in和not in
    5. 追加append
    6. 删除
    7. 循环

    1.按索引取值(正向取值+反向取值),即可存也可取

    name_list = ['kang', 'wei', 'lin']
    name_list[1] = 'li'
    print(name_list[1])
    

    li

    2.切片

    name_list = ['kang', 'wei', 'lin']
    print(name_list[0:2])
    
    

    ['kang', 'wei']

    3.长度(len)

    name_list = ['kang', 'wei', 'lin']
    print(len(name_list))
    

    3

    4.追加值 .append()

    name_list = ['name', 'wei', 'lin']
    name_list.append('haha')
    print(name_list)
    

    ['name', 'wei', 'lin', 'haha']

    5.删除(del)

    name_list = ['name', 'wei', 'lin']
    del name_list[0]
    print(name_list)
    

    ['wei', 'lin']

    7.循环(for)

    name_list = ['kang', 'wei', 'lin']
    
    for name in name_list:
        print(name)
    

    kang
    wei
    lin

    需要掌握

    1. insert
    2. pop
    3. remove
    4. count
    5. index
    6. clear
    7. copy
    8. extend
    9. reverse
    10. sort

    1.insert(增加值)

    name_list = ['kang', 'wei', 'lin']
    name_list.insert(0, 'yi')
    print(name_list)
    
    

    ['yi', 'kang', 'wei', 'lin']

    2.pop

    name_list = ['name', 'wei', 'lin']
    name_list.pop() # 默认删除最后一个
    print(name_list)
    
    
    

    ['name', 'wei']

    3.remove()

    name_list = ['name', 'wei', 'lin']
    print(name_list.remove('name'))#移除
    print(name_list)
    
    

    None
    ['wei', 'lin']

    4.count()

    name_list = ['name', 'wei', 'lin']
    print(name_list.count('wei')) #查看值的数量
    
    

    1

    5.index()

    name_list = ['kang', 'wei', 'yi']
    print(name_list.index('yi') # 查看对应索引
    
    

    2

    6.clear()

    name_list = ['kang', 'wei', 'lin']
    name_list.clear()  # 清空
    print(name_list)
    
    

    []

    7.copy()

    name_list = ['kang', 'wei', 'yi']
    print(name_list.copy()) # 复制
    
    

    ['kang', 'wei', 'yi']

    8.extend()

    name_list = ['name', 'wei', 'lin']
    name_list2 = ['kangkang']
    name_list.extend(name_list2) # 扩展name_lsit
    print(name_list)
    
    

    ['name', 'wei', 'lin', 'kangkang']

    9.reverse

    name_list = ['kang', 'wei', 'lin']
    name_list.sort() # 反转
    print(name_list)
    
    

    ['lin', 'wei', 'kang']

    10.sort()

    name_list = ['kang', 'wei', 'lin']
    name_list.sort
    print(name_list) # 排序
    
    
    

    ['kang', 'wei', 'lin']

    4.存一个值or多个值:多个值

    5.有序or无序:有序

    name_list = ['naem', 'wei', 'lin']
    print(id(name_list))
    name_list[2]= ''
    print(id(name_list))
    
    

    2192439595592
    2192439595592

    6.可变or不可变:可变数据类型

    字典类型内置方法

    1.用途:存多个值,但每一个值都有一个key与之对应,key对值有描述功能。多用于存的值表示的是不同的状态时,例如存的值有姓名、年龄、身高、体重、爱好。

    2.定义:{}内用逗号分隔开多个元素,每一个元素都是key:value的形式,value可以是任意数据类型,而key通常应该是字符串类型,但是key必须为不可变类型。

    dic = ['a': 1, 'b':2]
    print(type(dic))
    
    

    <class 'dict'>

    常用操作+内置方法

    优先掌握

    1. 按key存取值:可存可取
    2. 长度len
    3. 成员运算in和not in
    4. 删除del
    5. 键keys()、值values()、键值对items()
    6. 循环

    按key存取值:可存可取

    # dic之按key存取值
    dic = {'a': 1, 'b': 2}
    
    print(f"first dic['a']: {dic['a']}")
    
    dic['a'] = 3
    
    print(f"second dic['a']: {dic['a']}")
    first dic['a']: 1
    second dic['a']: 3
    
    

    2.长度len

    # dic之长度len
    dic = {'a': 1, 'b': 2}
    
    print(f"len(dic): {len(dic)}")
    len(dic): 2
    
    

    3.成员运算in和not in

    # dic之成员运算in和not in
    dic = {'a': 1, 'b': 2}
    
    print(f"'a' in dic: {'a' in dic}")
    print(f"1 in dic: {1 in dic}")
    'a' in dic: True
    1 in dic: False
    
    

    4.删除

    # dic之删除del
    dic = {'a': 1, 'b': 2}
    del dic['a']
    
    print(f"dic.get('a'): {dic.get('a')}")
    dic.get('a'): None
    # dic之删除pop()
    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.pop('b'): 2
    dic.get('a'): None
    # dic之删除popitem()
    dic = {'a': 1, 'b': 2}
    
    print(f"dic.popitem(): {dic.popitem()}")  # popitem() 方法随机返回并删除字典中的一对键和值(一般删除末尾对)。
    dic.popitem(): ('b', 2)
    
    

    5.键keys()、值values()、键值对items()

    # dic之键keys()、值values()、键值对items(),python2中取出的是列表(鸡蛋);python3中取出的是元组(鸡)
    dic = {'a': 1, 'b': 2}
    
    print(f"dic.keys(): {dic.keys()}")
    print(f"dic.values(): {dic.values()}")
    print(f"dic.items(): {dic.items()}")
    dic.keys(): dict_keys(['a', 'b'])
    dic.values(): dict_values([1, 2])
    dic.items(): dict_items([('a', 1), ('b', 2)])
    
    

    6.循环

    # dic之循环
    # dic是无序的,但是python3采用了底层优化算法,所以看起来是有序的,但是python2中的字典是无序
    dic = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    
    for k, v in dic.items():  # items可以换成keys()、values()
        print(k, v)
    a 1
    b 2
    c 3
    d 4
    
    

    需要掌握

    1. get
    2. update
    3. fromkeys
    4. setdefault

    1.get()

    # dic之get()
    dic = {'a': 1, 'b': 2}
    
    print(f"dic.get('a'): {dic.get('a')}")
    print(f"dic.get('c'): {dic.get('c')}")
    dic.get('a'): 1
    dic.get('c'): None
    
    

    2.update()

    # dic之update()
    dic1 = {'a': 1, 'b': 2}
    dic2 = {'c': 3}
    dic1.update(dic2)
    
    print(f"dic1: {dic1}")
    dic1: {'a': 1, 'b': 2, 'c': 3}
    
    

    3.fromkeys()

    # dic之fromkeys()
    dic = dict.fromkeys(['name', 'age', 'sex'], None)
    
    print(f"dic: {dic}")
    dic: {'name': None, 'age': None, 'sex': None}
    
    

    4.setdefault()

    # dic之setdefault(),有指定key不会改变值;无指定key则改变值
    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}")
    dic.setdefault('a'): 1
    dic: {'a': 1, 'b': 2}
    dic.setdefault('c'): 3
    dic: {'a': 1, 'b': 2, 'c': 3}
    
    

    集合类型内置方法(set)

    1.集合可以理解成一个集合体,学习Python的学生可以是一个集合体;学习Linux的学生可以是一个集合体。

    2.用途:用于关系运算的集合体,由于集合内的元素无序且集合元素不可重复,因此集合可以去重,但是去重后的集合会打乱原来元素的顺序。

    3.定义:{}内用逗号分隔开多个元素,每个元素必须是不可变类型。

    s = {1, 2, 1, 'a'}  # s = set({1,2,'a'})
    
    print(s)
    
    

    {1, 2, 'a'}

    常用操作+内置方法

    优先掌握

    1. 长度len
    2. 成员运算in和not in
    3. |并集、union
    4. &交集、intersection
    5. -差集、difference
    6. ^对称差集、symmetric_difference
    7. ==
    8. 父集:>、>= 、issuperset
    9. 子集:<、<= 、issubset

    长度len

    # set之长度len
    s = {1, 2, 'a'}
    
    print(f"len(s): {len(s)}")
    len(s): 3
    
    

    2.成员运算in和not in

    # set之成员运算in和not in
    s = {1, 2, 'a'}
    
    print(f"1 in s: {1 in s}")
    1 in s: True
    
    

    集合类型内置运算-集合运算英文.jpg?x-oss-process=style/watermark

    3.|并集

    # str之|并集
    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    
    print(f"pythoners|linuxers: {pythoners|linuxers}")
    print(f"pythoners.union(linuxers): {pythoners.union(linuxers)}")
    pythoners|linuxers: {'egon', 'tank', 'kevin', 'jason', 'nick', 'sean'}
    pythoners.union(linuxers): {'egon', 'tank', 'kevin', 'jason', 'nick', 'sean'}
    
    

    4.&交集

    # str之&交集
    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    
    print(f"pythoners&linuxers: {pythoners&linuxers}")
    print(f"pythoners.intersection(linuxers): {pythoners.intersection(linuxers)}")
    pythoners&linuxers: {'nick'}
    pythoners.intersection(linuxers): {'nick'}
    
    

    5.-差集

    # str之-差集
    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    
    print(f"pythoners-linuxers: {pythoners-linuxers}")
    print(f"pythoners.difference(linuxers): {pythoners.difference(linuxers)}")
    pythoners-linuxers: {'tank', 'jason', 'sean'}
    pythoners.difference(linuxers): {'tank', 'jason', 'sean'}
    
    

    6.^对称差集

    # str之^对称差集
    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)}")
    pythoners^linuxers: {'egon', 'tank', 'kevin', 'jason', 'sean'}
    pythoners.symmetric_difference(linuxers): {'egon', 'tank', 'kevin', 'jason', 'sean'}
    
    

    7.==

    # str之==
    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}")
    pythoners==linuxers: False
    javers==linuxers: True
    
    

    8.父集:>、>=

    # str之父集:>、>=
    pythoners = {'jason', 'nick', 'tank', 'sean'}
    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.issuperset(javaers): {pythoners.issuperset(javaers)}")
    pythoners>linuxers: False
    pythoners>=linuxers: False
    pythoners>=javaers: True
    pythoners.issuperset(javaers): True
    
    

    9.子集:<、<=

    # str之子集:<、<=
    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    javaers = {'jason', 'nick'}
    
    print(f"pythoners<linuxers: {pythoners<linuxers}")
    print(f"pythoners<=linuxers: {pythoners<=linuxers}")
    print(f"javaers.issubset(javaers): {javaers.issubset(javaers)}")
    pythoners<linuxers: False
    pythoners<=linuxers: False
    javaers.issubset(javaers): True
    
    

    1.2 需要掌握(**)

    1.add()

    # set之add()
    s = {1, 2, 'a'}
    s.add(3)
    
    print(s)
    {1, 2, 3, 'a'}
    
    

    2.remove()

    # set之remove()
    s = {1, 2, 'a'}
    s.remove(1)
    
    print(s)
    {2, 'a'}
    
    

    3.difference_update()

    # str之difference_update()
    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    pythoners.difference_update(linuxers)
    
    print(f"pythoners.difference_update(linuxers): {pythoners}")
    pythoners.difference_update(linuxers): {'tank', 'jason', 'sean'}
    
    

    4.discard()

    # set之discard()
    s = {1, 2, 'a'}
    # s.remove(3)  # 报错
    s.discard(3)
    
    print(s)
    {1, 2, 'a'}
    
    

    5.isdisjoint()

    # set之isdisjoint(),集合没有共同的部分返回True,否则返回False
    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    pythoners.isdisjoint(linuxers)
    
    print(f"pythoners.isdisjoint(linuxers): {pythoners.isdisjoint(linuxers)}")
    pythoners.isdisjoint(linuxers): False
    
    
  • 相关阅读:
    兔子数
    忠诚
    mysql字段名与关键字冲突(near "to":syntax error)
    C/C++使用心得:enum与int的相互转换
    ubuntu重新安装 apache2
    ubuntu 删除mysql
    Notepad++ 代码格式化
    linux文件字符集转换(utf8-gb2312)
    字符编码详解——彻底理解掌握编码知识,“乱码”不复存在
    c语言判断是否是utf8字符串,计算字符个数
  • 原文地址:https://www.cnblogs.com/kangwy/p/11305619.html
Copyright © 2020-2023  润新知