一、列表类型内置方法(list)
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 优先掌握(*****)
-
按索引取值(正向取值+反向取值),即可存也可以取
-
切片
-
长度len
-
成员运算in和not in
-
追加append
-
删除del
-
循环
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 需要掌握(****)
-
insert
-
pop
-
remove
-
count
-
index
-
clear
-
copy
-
extend
-
reverse
-
sort
1.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']
2.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']
3.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']
4.count()
# list之count()
name_list = ['nick', 'jason', 'tank', 'sean']
print(f"name_list.count('nick'): {name_list.count('nick')}")
name_list.count('nick'): 1
5.index()
# list之index()
name_list = ['nick', 'jason', 'tank', 'sean']
print(f"name_list.index('nick'): {name_list.index('nick')}")
name_list.index('nick'): 0
6.clear()
# list之clear()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.clear()
print(f"name_list: {name_list}")
name_list: []
7.copy()
# list之copy()
name_list = ['nick', 'jason', 'tank', 'sean']
print(f"name_list.copy(): {name_list.copy()}")
name_list.copy(): ['nick', 'jason', 'tank', 'sean']
8.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']
9.reverse()
# list之reverse()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.reverse()
print(f"name_list: {name_list}")
name_list: ['sean', 'tank', 'jason', 'nick']
10.sort()
# 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_list: ['jason', 'nick', 'sean', 'tank']
name_list_reverse: ['tank', 'sean', 'nick', 'jason']
4.存一个值or多个值:多个值
5.有序or无序:有序
hobby_list = ['read', 'run', 'girl']
print(f'first:{id(hobby_list)}')
hobby_list[2] = ''
print(f'second:{id(hobby_list)}')
first:4522187016
second:4522187016
6.可变or不可变:可变数据类型