欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python-day8(正式学习)

程序员文章站 2023-11-21 22:38:10
列表内置方法,元组内置方法,字典内置方法,集合内置方法(手都写断...) ......

目录

列表类型内置方法

常用操作+内置方法

优先掌握(***)

按索引取值,可存可取

# list之索引取值
name_list = ['nick', 'jason', 'tank', 'sean']
name_list[0] = 'nick handsom'
# name_list[1000] = 'tank sb'  # 报错

print(f"name_list[0]: {name_list[0]}")
name_list[0]: nick handsom

切片

# list之切片
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"name_list[0:3:2]: {name_list[0:3:2]}")
name_list[0:3:2]: ['nick', 'tank']

长度

# list之长度
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"len(name_list): {len(name_list)}")

成员运算in和not in

# list之成员运算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}")
'tank sb' in name_list: false
'nick handsome' not in name_list: true

追加值

# list之追加值
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.append('tank sb')

print(f"name_list: {name_list}")
name_list: ['nick', 'jason', 'tank', 'sean', 'tank sb']

删除

# list之删除,也可以删除一个切片范围内的值,甚至可以删除整个列表
name_list = ['nick', 'jason', 'tank', 'sean']
del name_list[2]

print(f"name_list: {name_list}")
name_list: ['nick', 'jason', 'sean']

循环

# list之循环
name_list = ['nick', 'jason', 'tank', 'sean']

for name in name_list:
    print(name)
nick
jason
tank
sean

需要掌握(**)

insert()(在指定索引处插入一个元素)

# list之insert()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.insert(1, 'handsome')

print(f"name_list: {name_list}")
name_list: ['nick', 'handsome', 'jason', 'tank', 'sean']

pop()(删除指定索引的元素,执行方法时会返回这个元素,不指定索引时默认最后一个)

# list之pop(),pop()默认删除最后一个元素,并返回一个被删除的元素
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"name_list.pop(1): {name_list.pop(1)}") 
print(f"name_list: {name_list}")
name_list.pop(1): jason
name_list: ['nick', 'tank', 'sean']

remove()(删除首个符合你删除要求--和你给出的值相等--的元素)

# list之remove()
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"name_list.remove('nick'): {name_list.remove('nick')}")
print(f"name_list: {name_list}")
name_list.remove('nick'): none
name_list: ['jason', 'tank', 'sean']

count()(计数器,给出你输入元素值的出现次数)

 list之index()
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"name_list.index('nick'): {name_list.index('nick')}")
name_list.index('nick'): 0

index()(查找到给定元素值的索引)

# list之index()
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"name_list.index('nick'): {name_list.index('nick')}")
name_list.index('nick'): 0

clear()(清空整个列表的所有元素)

# list之clear()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.clear()

print(f"name_list: {name_list}")
name_list: []

copy()(复制整个列表,用于其他操作)

# list之copy()
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"name_list.copy(): {name_list.copy()}")
name_list.copy(): ['nick', 'jason', 'tank', 'sean']

extend()(把另一个列表中的所有元素扩展到指定列表中)

# list之extend()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list2 = ['nick handsome']
name_list.extend(name_list2)

print(f"name_list: {name_list}")
name_list: ['nick', 'jason', 'tank', 'sean', 'nick handsome']

reverse()(列表中所有元素反转)

# list之reverse()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.reverse()

print(f"name_list: {name_list}")
name_list: ['sean', 'tank', 'jason', 'nick']

sort()(给列表排序,有两个参数,key是比较值,reverse是倒序,这两个参数也可不加)

# list之sort(),使用sort列表的元素必须是同类型的
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.sort()

print(f"name_list: {name_list}")

name_list.sort(reverse=true)
print(f"name_list_reverse: {name_list}")
补:
name=[1,2,3,4,5]
print(name.sort(key=lambda k:-k))
print(name)
print(name.sort(key=lambda k:-k,reverse=true))
print(name)
name_list: ['jason', 'nick', 'sean', 'tank']
name_list_reverse: ['tank', 'sean', 'nick', 'jason']

none
[5, 4, 3, 2, 1]
none
[1, 2, 3, 4, 5]

存一个值or多个值

多个值

有序or无序

有序

可变or不可变

可变

元组类型内置方法

定义

与列表相似,用()

name_str = ('egon')  # ()只是普通包含的意思
name_tuple = ('egon',)

print(f"type(name_str): {type(name_str)}")
print(f"type(name_tuple): {type(name_tuple)}")
type(name_str): <class 'str'>
type(name_tuple): <class 'tuple'>

常用操作+内置方法

优先掌握

索引取值

# tuple之索引取值
name_tuple = ('nick', 'jason', 'tank', 'sean')
# name_tuple[0] = 'nick handsom'  # 报错

print(f"name_tuple[0]: {name_tuple[0]}")
name_tuple[0]: nick

切片

# tuple之切片
name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"name_tuple[1:3:2]: {name_tuple[1:3:2]}")
name_tuple[1:3:2]: ('jason',)

长度

# tuple之长度
name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"len(name_tuple): {len(name_tuple)}")
len(name_tuple): 4

成员运算

# tuple之成员运算
name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"'nick' in name_tuple: {'nick' in name_tuple}")
'nick' in name_tuple: true

循环

# tuple之循环
name_tuple = ('nick', 'jason', 'tank', 'sean')

for name in name_tuple:
    print(name)
nick
jason
tank
sean

count()

# tuple之count()
name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"name_tuple.count('nick'): {name_tuple.count('nick')}")
name_tuple.count('nick'): 1

index()

# tuple之index()
name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"name_tuple.index('nick'): {name_tuple.index('nick')}")
name_tuple.index('nick'): 0

一个值or多个值

一个值

有序or无序

有序

可变or不可变

不可变数据类型

字典类型内置方法

常用操作+内置方法

优先掌握(***)

按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

长度len

# dic之长度len
dic = {'a': 1, 'b': 2}

print(f"len(dic): {len(dic)}")
len(dic): 2

成员运算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

删除

# 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)

键key(),值value(),键值对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)])

循环

# 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

需要掌握(**)

get()(根据键返回值,若无则返回none)

# 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

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}

fromkeys()(快速建一个字典,前提是值都相同)

# dic之fromkeys()
dic = dict.fromkeys(['name', 'age', 'sex'], none)

print(f"dic: {dic}")
dic: {'name': none, 'age': none, 'sex': none}

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}

存一个值or多个值

多个值,key是不可变值

有序or无序

无序

可变or不可变

可变数据类型

集合类型内置方法

定义

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

print(f"s: {s}")
s: {1, 2, 'a'}

常用操作+内置方法

优先掌握

长度

# set之长度len
s = {1, 2, 'a'}

print(f"len(s): {len(s)}")
len(s): 3

成员运算in和not in

# set之成员运算in和not in
s = {1, 2, 'a'}

print(f"1 in s: {1 in s}")
1 in s: true

|并集

# 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'}

&交集

# 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'}

-差集

# 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'}

^对称差集

# 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'}

==

# 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

父集:>,>=

# 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): 

子集:<,<=

# 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

需要掌握(**)

add()

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

print(s)
{1, 2, 3, 'a'}

remove()

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

print(s)
{2, 'a'}

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'}

discard()

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

print(s)
{1, 2, 'a'}

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

存一个值or多个值

多个值,值为不可变类型

有序or无序

无序

可变or不可变

可变数据类型