基本数据类型列表list(增删改查)
•list 专门是用来存储大量的数据的数据类型
•在python中使用[]中括号来表示列表
•列表是可变的数据类型,内部元素没有任何要求
•列表: 能装东西的东西
lis = ["张三","李四","wangwu",{1,2,3},["uan","gpc"]]
print (lis)
[‘张三’, ‘李四’, ‘wangwu’, {1, 2, 3}, [‘uan’, ‘gpc’]]
添加元素
•append()追加
•insert()插入
•extend()迭代添加
# 1 添加元素
# append()追加
a = ["唐僧","沙悟净","猪八戒"]
print (a)
a.append("孙悟空")
print(a)
[‘唐僧’, ‘沙悟净’, ‘猪八戒’]
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’]
# 2,插入 insert()
a = ["唐僧","沙悟净","猪八戒"]
print(a)
a.insert(1,"孙悟空")
print(a)
[‘唐僧’, ‘沙悟净’, ‘猪八戒’]
[‘唐僧’, ‘孙悟空’, ‘沙悟净’, ‘猪八戒’]
# 迭代添加 extend ()
a = ["唐僧","沙悟净","猪八戒"]
print(a)
a.extend(["孙悟空","白龙马"])
print(a)
a.extend("葛鹏程")
print(a)
[‘唐僧’, ‘沙悟净’, ‘猪八戒’]
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’, ‘葛’, ‘鹏’, ‘程’]
删除元素
•remove()删除一个元素
•pop(1) 索引指标删除0,1,2,3 当()括号为空时默认删除最后一个
•clear() 清空
•del删除
a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
a.remove("唐僧")
print (a)
a.remove("如来")#字符不存在就报错
print (a)
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
ValueError Traceback (most recent call last)
in
3 a.remove(“唐僧”)
4 print (a)
----> 5 a.remove(“如来”)#字符不存在就报错
6 print (a)
ValueError: list.remove(x): x not in list
a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
a.pop()
print (a)
a.pop(1)
print (a)
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’]
[‘唐僧’, ‘猪八戒’, ‘孙悟空’]
a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
a.clear()
print (a)
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[]
a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
del a[0]
print (a)
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
修改
•列表通过索引修改
a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
a[0]="如来"
print(a)
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘如来’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
a[1:3]=["如来"]
print(a)
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘唐僧’, ‘如来’, ‘孙悟空’, ‘白龙马’]
a = ["唐僧","沙悟净",["马化腾","ma云","葛鹏程"],"猪八戒","孙悟空","白龙马"]
a[2][1] = "马云"
print(a)
[‘唐僧’, ‘沙悟净’, [‘马化腾’, ‘马云’, ‘葛鹏程’], ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
a = ["唐僧","沙悟净",["马化腾","ma云","葛鹏程"],"猪八戒","孙悟空","白龙马"]
a[2][1] = a[2][1].replace("ma","马")
print(a)
[‘唐僧’, ‘沙悟净’, [‘马化腾’, ‘马云’, ‘葛鹏程’], ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
查询
•列表通过索引和切片进行查询
•使用for循环进行查询
a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a)
print(a[2::2])
print (a[1])
print(a[::-2])
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘猪八戒’, ‘白龙马’]
沙悟净
[‘白龙马’, ‘猪八戒’, ‘唐僧’]
a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
for b in a:
print (b)
唐僧
沙悟净
猪八戒
孙悟空
白龙马
a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
for b in a:
for c in b:
print (c)
唐
僧
沙
悟
净
猪
八
戒
孙
悟
空
白
龙
马
a = ["唐僧","沙悟净","猪八戒","孙悟空","白龙马"]
print (a[3][0])
孙
a = ["唐僧","沙悟净",["马化腾","马云","葛鹏程"],"猪八戒","孙悟空","白龙马"]
print (a[2][0])
print (a[2][0][1])
马化腾
化
# range 数数
for i in range (0,11):
print (i)
0
1
2
3
4
5
6
7
8
9
10
for i in range (10):
print ("我爱你",i)
我爱你 0
我爱你 1
我爱你 2
我爱你 3
我爱你 4
我爱你 5
我爱你 6
我爱你 7
我爱你 8
我爱你 9
a = ["唐僧","沙悟净","马化腾","马云","葛鹏程","猪八戒","孙悟空","白龙马"]
for b in range(len(a)):
print (a[b])
唐僧
沙悟净
马化腾
马云
葛鹏程
猪八戒
孙悟空
白龙马
a = ["唐僧",",,","沙悟净","马化腾","马云","葛鹏程","猪八戒","孙悟空","白龙马"]
print(a)
a[1]=a[0]
print (a)
[‘唐僧’, ‘,’, ‘沙悟净’, ‘马化腾’, ‘马云’, ‘葛鹏程’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
[‘唐僧’, ‘唐僧’, ‘沙悟净’, ‘马化腾’, ‘马云’, ‘葛鹏程’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]
# 3,replace ( ) 字符串替换
test = "我 、喜 、欢 打 游戏"
print(test)
test1=test.replace("游戏","看书")
print (test1)
test2 = test.replace(" ","") #去掉所有空格
print (test2)
test2 = test.replace("、","
")
print (test2)
我 、喜 、欢 打 游戏
我 、喜 、欢 打 看书
我、喜、欢打游戏
我
喜
欢 打 游戏
a = ["呵呵","哈哈","嘻嘻","嘿嘿"]
b = ["3","4","5","6"]
d = ["呵呵11","哈哈11","嘻嘻11","嘿嘿11"]
c =[]
c.extend(d)
for i in range (4):
text =a [i]
times=int(b[i])
for time in range(times):
gpc1 = (text+str(time+1))
c.append(gpc1)
print(c)
[‘呵呵11’, ‘哈哈11’, ‘嘻嘻11’, ‘嘿嘿11’, ‘呵呵1’, ‘呵呵2’, ‘呵呵3’, ‘哈哈1’, ‘哈哈2’, ‘哈哈3’, ‘哈哈4’, ‘嘻嘻1’, ‘嘻嘻2’, ‘嘻嘻3’, ‘嘻嘻4’, ‘嘻嘻5’, ‘嘿嘿1’, ‘嘿嘿2’, ‘嘿嘿3’, ‘嘿嘿4’, ‘嘿嘿5’, ‘嘿嘿6’]
# 迭代添加 extend ()
a = ["唐僧","沙悟净","猪八戒"]
print(a)
a.extend(["孙悟空","白龙马"])
print(a)
[‘唐僧’, ‘沙悟净’, ‘猪八戒’]
[‘唐僧’, ‘沙悟净’, ‘猪八戒’, ‘孙悟空’, ‘白龙马’]