列表类型内置方法
优先掌握(*****)
- 按索引取值(正向取值+反向取值),即可存也可以取
- 切片
- 长度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
需要掌握(****)
- 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']