按索引取值
name_list=['hh','开心','happy']
print(name_list[0])
hh
切片
print(name_list[0:2:1])
['hh', '开心']
长度len
print(len(name_list))
3
成员判断in or not in
print('hh' not in name_list)
True
追加append
name_list.append('11')
print(name_list)
print(name_list.append('11'))
['开心', 'happy', '11', '11', '11']
None
删除del
del name_list[0]
print(name_list)
['happy', '11', '11', '11', '11']
循 环for
for i in name_list:
print(i)
happy
11
11
11
11
插入元素insert
name_list.insert(2,'4')
print(name_list)
['happy', '11', '4', '11', '11', '11']
pop删除
name_list=['开心', 'happy', '11', '11', '11']
name_list.pop(1)
print(name_list)
name_list.pop()#默认去除最后一个
print(name_list)
['开心', '11', '11', '11']
['开心', '11', '11']
remove去除
print(name_list)
name_list.remove('11')#根据元素的值来删除列表中的元素。而pop方法是根据元素的索引来删除列表中的元素
print(name_list)
['happy', '11', '11']
['happy', '11']
count计数
name_list=['开心', 'happy', '4', '11', '11']
print(name_list.count('11'))
2
index索引
print(name_list.index('11'))
3
clear清空
name_list.clear()
print(name_list)
[]
copy复制
name_list=['2','sfds','dskd']
print(name_list.copy())
['2', 'sfds', 'dskd']
extend扩展
name_list2=['00','fdfj']
name_list.extend(name_list2)
print(name_list)
['2', 'sfds', 'dskd', '00', 'fdfj']
reverse#翻转
name_list.reverse()
print(name_list)
['fdfj', '00', 'dskd', 'sfds', '2']
sort排序
num=['2','4','0']
num.sort()
print(num)
['0', '2', '4']