int会默认去除空格
增
1.APPEND
list = [1,'a','b',2,3,'老男孩'] list.append('alex') print(list)
print(li.append('alex') return:none这个是动作,没有返回值
2.INSERT
list = [1,'a','b',2,3,'老男孩'] list.insert(2,'kate') print(list)
3.EXTEND
li = [1,'a','b',2,3,'老男孩'] li.extend('abc') print(li)
##HR添加新员工
name_list = ['kate','nero','jake'] while True: name = input('请添加员工姓名:按Q/q退出') if name.upper() == 'Q':break else: name_list.append(name) print('已成功添加新员工%s % name') print(name_list)
删
#POP唯一一个有返回值的,按索引删,只能删一个元素
li = ['kate','nero','jake',1,2,3] li.pop() print(li)
空格默认删除最后一个
li = ['kate','nero','jake',1,2,3] li.pop(1) print(li)
['kate', 'jake', 1, 2, 3]
li = ['kate','nero','jake',1,2,3] print(li.pop(1))
返回值 nero
#remove按元素删,默认就删一个
li = ['kate','nero','jake',1,2,3] li.remove('kate') print(li)
# clear 清空内容
li = ['kate','nero','jake',1,2,3] li.clear() print(li)
return[]
# del 删除列表
li = ['kate','nero','jake',1,2,3] del li[0:2] print(li)
# 跳着删
li = ['kate','nero','jake',1,2,3] del li[0:5:2] print(li)
['nero', 1, 3]
改
1.按照索引改
li = ['kate','nero','jake',1,2,3] li[1]= 'KATE' print(li)
['kate', 'KATE', 'jake', 1, 2, 3]
print(li[1]) 只是打印元素 KATE
2.按切片去改
li = ['kate','nero','jake',1,2,3] li[:3] = '0' print(li)
li = ['kate','nero','jake',1,2,3] li[:3] = 'nerosb' print(li)
['n', 'e', 'r', 'o', 's', 'b', 1, 2, 3]
li = ['kate','nero','jake',1,2,3] li[:3] = [11,22,33,44] print(li)
把原来位置清空,再放入新的元素一个一个添加进去
[11, 22, 33, 44, 1, 2, 3]
查:索引,切片步长,查看
li = ['kate','nero','jake',1,2,3] print(li[:3]) for i in li: print(i)
kate
nero
jake
1
2
3
#sort: 正向排序,从小到大
li = [2,7,9,0,2,3,4,5,5] li.sort() print(li)
[0, 2, 2, 3, 4, 5, 5, 7, 9]
#反向排序,从大到小
li = [2,7,9,0,2,3,4,5,5] li.sort(reverse=True) print(li)
[9, 7, 5, 5, 4, 3, 2, 2, 0]
# 翻转 reverse
li = [2,7,9,0,2,3,4,5,5] li.reverse() print(li)
[5, 5, 4, 3, 2, 0, 9, 7, 2]
# len 长度
li = [2,7,9,0,2,3,4,5,5] print(len(li))
9
# count
li = [2,7,9,0,2,3,4,5,5] print(li.count(2))
2
# index 通过元素找索引
li = [2,7,9,0,2,3,4,5,5] print(li.index(2))
0
列表的嵌套
li = [1,2,'alex',['100','wusir',99],22] li[2]=li[2].capitalize() print(li) li[3][1] = li[3][1].upper() print(li) li[3][2] = li[3][2] + 1 print(li)
1.将alex变成Alex
2.将wusir变成WUSIR
3.将99变成100
[1, 2, 'Alex', ['100', 'wusir', 99], 22]
[1, 2, 'Alex', ['100', 'WUSIR', 99], 22]
[1, 2, 'Alex', ['100', 'WUSIR', 100], 22]
#元组:不能增删改,只能查,通过切片,索引,for循环来查
tu = (1,2,'alex','oldboy') print(tu[:2]) print(tu[2]) for i in tu: print(i)
#儿子不能改,孙子可能改
tul = (1,2,'alex',[1,'taibai'],'1,2,3','oldboy') tul[3].append('日天') print(tul)
元组中含列表,只能改列表
#RANGE:
1.当成数字列表range(1,10,2)范围加步长
for i in range(1,100,1): print(i)
打印1-100
2.还可以倒着取值
for i in range(100,0,-1):
print(i)
打印100-1倒叙
3.len
li = input('请输入内容') for i in range(0,len(li)): print(i)
#Join:iterable 对可迭代的对象进行操作,字符串,元组,列表都可用,但是字符串用最方便
s = '*'.join('老男孩') print(s)
老*男*孩
# split str转list
s = 'wusir alex taibai' print(s.split())
['wusir', 'alex', 'taibai'] 字符串变列表
# join list转str
s =['wusir', 'alex', 'taibai'] print(''.join(s))
wusiralextaibai 列表变字符串
思考题:
1.把每一个元素依次打印,两种方法
第一种:
li = [1,2,3,['alex','wusir','老男孩'],4] for i in li: if type(i) == list: for k in li[3]: print(k) else: print(i)
第二种: