8.5 day8

列表类行内置方法

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

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

# my_girl_friend = list(['jason','tank','sean'])
my_girl_friend = ['jason', 'tank', 'sean']

print(f"my_girl_friend: {my_girl_friend}")
my_girl_friend: ['jason', 'tank', 'sean']
l = list('hello nick')
print(f"l: {l}")
l: ['h', 'e', 'l', 'l', 'o', ' ', 'n', 'i', 'c', 'k']

3.常用操作+内置方法:常用操作和内置方法分为优先掌握(今天必须得记住)、需要掌握(一周内记住)、其他操作(了解)三个部分。

1.1 优先掌握(***)

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

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

# 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

2.切片

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

3.长度

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

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

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

5.追加值

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

6.删除

# list之删除
name_list = ['nick', 'jason', 'tank', 'sean']
del name_list[2]

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

7.循环

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

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

1.2 需要掌握(**)

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

元组类行内置方法

元组是不可变的列表,即元组的值不可更改,因此元组一般只用于只存不取的需求。也因此元组可以被列表取代掉,所以元组相比较列表使用的很少。元组相比较列表的优点为:列表的值修改后,列表的结构将会发生改变,而元组只需要存储,因此列表在某种程度上而言需要占用更多的内存。但是目前工业上内存已经不是问题了,所以工业上元组一般不会使用。

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

2.定义:在()内可以有多个任意类型的值,逗号分隔元素

# my_girl_friend = tuple(('jason','tank','sean'))
my_girl_friend = ('jason', 'tank', 'sean')

print(f"my_girl_friend: {my_girl_friend}")
my_girl_friend: ('jason', 'tank', 'sean')
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'>

3.常用操作+内置方法:常用操作和内置方法:

1.1 优先掌握(***)

  1. 索引取值
  2. 切片(顾头不顾尾,步长)
  3. 长度len
  4. 成员运算in和not in
  5. 循环
  6. count
  7. index

1.索引取值

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

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

2.切片(顾头不顾尾,步长)

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

3.长度

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

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

4.成员运算

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

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

5.循环

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

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

6.count()

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

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

7.index()

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

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

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

5.有序or无序:有序

name_tuple = ('nick',)
print(f'first:{id(name_tuple)}')
first:4394454152

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

二、元组和列表的区别

l = ['a', 'b', 'c']
print(f"id(l[0]): {id(l[0])}")
l[0] = 'A'
print(f"id(l[0]): {id(l[0])}")
id(l[0]): 4357367208
id(l[0]): 4357775176

列表可变的原因是:索引所对应的值的内存地址是可以改变的

元组不可变得原因是:索引所对应的值的内存地址是不可以改变的,或者反过来说,只要索引对应值的内存地址没有改变,那么元组是始终没有改变的。

t1 = (['a', 'b', 'c'], 'wc', 'office')

print(f"id(t1[0]): {id(t1[0])}")
print(f"id(t1[1]): {id(t1[1])}")
print(f"id(t1[2]): {id(t1[2])}")

t1[0][0] = 'A'
print(f"t1[0][0]: {t1[0][0]}")
print(f"id(t1[0]): {id(t1[0])}")
print(f"t1: {t1}")
id(t1[0]): 4394709960
id(t1[1]): 4374626968
id(t1[2]): 4394453568
t1[0][0]: A
id(t1[0]): 4394709960
t1: (['A', 'b', 'c'], 'wc', 'office')

字典类行内置方法

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

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

dic = {'a': 1, 'b': 2}  # dic = dict({'a':1,'b':2})

print(f"dic: {dic}")
dic: {'a': 1, 'b': 2}
dic = dict(a=1, b=2, c=3)

print(f"dic: {dic}")
dic: {'a': 1, 'b': 2, 'c': 3}
dic = {1: 'a', 0: 'b'}

print(f"dic[0]: {dic[0]}")  # 无法区分dic是列表,还是字典,并且key不再具有描述信息
dic[0]: b
dic = {[1,2]: 'a', 0: 'b'}  # 报错

3.常用操作+内置方法:常用操作和内置方法分为优先掌握(今天必须得记住)、需要掌握(一周内记住)两个部分。

1.1 优先掌握(***)

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

1.按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.2 需要掌握(**)

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

集合类型内置方法

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

pythoners = ['jason', 'nick', 'tank', 'sean']
linuxers = ['nick', 'egon', 'kevin']

# 即报名pythoners又报名linux的学生
py_li_list = []
for stu in pythoners:
    if stu in linuxers:
        py_li_list.append(stu)
print(f"pythoners and linuxers: {py_li_list}")
pythoners and linuxers: ['nick']

上述的列表方式求两个集合体的关系运算非常复杂,因此有了我们的集合数据类型。

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

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

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

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

for i in s:
    print(i)
1
2
c
a
s = set('hello')

print(f"s: {s}")
s: {'e', 'o', 'h', 'l'}

3.常用操作+内置方法:常用操作和内置方法分为优先掌握(今天必须得记住)、需要掌握(一周内记住)两个部分。

1.1 优先掌握(***)

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

1.长度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

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
  2. remove
  3. difference_update
  4. discard
  5. isdisjoint
上一篇:53-文件的三种打开方式


下一篇:Python学习笔记11—函数