列表操作
c=['cx','zrd','ajt','dne'] #定义一个列表,有4个元素
#增删改查
print(c[3]) #从0计数
测试
D:\python\python.exe D:/untitled/dir/for.py
dne
Process finished with exit code 0
取连续俩只,左包括;右不包括
c=['cx','zrd','ajt','dne'] #定义一个列表,有4个元素
#增删改查
#查
print(c[1:3]) #取zad和ajt值,左包括,右不包括
测试切片
D:\python\python.exe D:/untitled/dir/for.py
['zrd', 'ajt']
Process finished with exit code 0
取到最后一个元素的方法
c=['cx','zrd','ajt','dne'] #定义一个列表,有4个元素
#增删改查
#查
print(c[1:]) #冒号后什么也不加
测试
D:\python\python.exe D:/untitled/dir/for.py
['zrd', 'ajt', 'dne']
取到倒数第二个
c=['cx','zrd','ajt','dne'] #定义一个列表,有4个元素
#增删改查
#查
print(c[1:-1]) #取zad和ajt
测试
D:\python\python.exe D:/untitled/dir/for.py
['zrd', 'ajt']
从左到右一个个去取;设置步长
c=['cx','zrd','ajt','dne'] #定义一个列表,有4个元素
#增删改查
#查
print(c[0:-1:2]) #设置步长为2
测试
D:\python\python.exe D:/untitled/dir/for.py
['cx', 'ajt']
按顺序去dne与zrd;-表示从右到左
c=['cx','zrd','ajt','dne','chenxi'] #定义一个列表,有4个元素
print(c[3::-2])
测试
D:\python\python.exe D:/untitled/dir/for.py
['dne', 'zrd']
Process finished with exit code 0
列表添加元素
c=['cx','zrd','ajt','dne','chenxi'] #定义一个列表,有4个元素
c.append('hshs') #追加一个元素
print(c)
测试
D:\python\python.exe D:/untitled/dir/for.py
['cx', 'zrd', 'ajt', 'dne', 'chenxi', 'hshs']
列表插入元素到下标2
c=['cx','zrd','ajt','dne','chenxi'] #定义一个列表,有4个元素
c.insert(1,'hshs')
print(c)
测试
D:\python\python.exe D:/untitled/dir/for.py
['cx', 'hshs', 'zrd', 'ajt', 'dne', 'chenxi']
Process finished with exit code 0
列表元素替换
c=['cx','zrd','ajt','dne','chenxi'] #定义一个列表,有4个元素
# c.append('hshs')
c[1]='cfsd'
print(c)
测试
D:\python\python.exe D:/untitled/dir/for.py
['cx', 'cfsd', 'ajt', 'dne', 'chenxi']
Process finished with exit code 0
列表元素多个同时替换
c=['cx','zrd','ajt','dne','chenxi'] #定义一个列表,有4个元素
# c.append('hshs')
# c[1]='cfsd'
# print(c)
c[1:3]=['c','b'] #注意包左,不包右,替换zrd与ajt
print(c)
测试
D:\python\python.exe D:/untitled/dir/for.py
['cx', 'c', 'b', 'dne', 'chenxi']
Process finished with exit code 0
列表元素步长替换zrd与dne
c=['cx','zrd','ajt','dne','chenxi'] #定义一个列表,有4个元素
c[1:5:2]=['c','b']
print(c)
测试
D:\python\python.exe D:/untitled/dir/for.py
['cx', 'c', 'ajt', 'b', 'chenxi']
元素删除
c=['cx','zrd','ajt','dne','chenxi'] #定义一个列表,有4个元素
# c.append('hshs')
# c[1]='cfsd'
# print(c)
c.remove('cx') #删除列表元素为cx的
print(c)
测试
D:\python\python.exe D:/untitled/dir/for.py
['zrd', 'ajt', 'dne', 'chenxi']
Process finished with exit code 0
删除下标,并且取出删除的值
c=['cx','zrd','ajt','dne','chenxi'] #定义一个列表,有4个元素
b=c.pop(1)
print(c)
print(b)
测试
D:\python\python.exe D:/untitled/dir/for.py
['cx', 'ajt', 'dne', 'chenxi']
zrd
删除真个变量
c=['cx','zrd','ajt','dne','chenxi'] #定义一个列表,有4个元素
del c
print(c)
测试
D:\python\python.exe D:/untitled/dir/for.py
Traceback (most recent call last):
File "D:/untitled/dir/for.py", line 46, in print(c)
NameError: name 'c' is not defined
Process finished with exit code 1
根据元素查下标
c=['cx','zrd','danier','dne','chenxi'] #定义一个列表,有5个元素
print(c.index('danier'))
测试
D:\python\python.exe D:/untitled/dir/for.py
2
Process finished with exit code 0
取第二个zrd元素
c=['cx','zrd','danier','dne','chenxi','ajt','whd','zrd','qwe'] #定义一个列表,有4个元素
first_lg_index= c.index("zrd") #取大列表zrd位置
print(first_lg_index)
little_list = c[first_lg_index+1:]# 切片取小列表
second_lg_index = little_list.index("zrd")#取第二个列表里zrd的位置
print(second_lg_index)
second_lg_index_in_dig_list = first_lg_index + second_lg_index +1 #通过第一个zrd在大列表的位置,加上第二个zrd在第二个位置加1计算出第二个zrd的下标
print(second_lg_index_in_dig_list)
print(c[second_lg_index_in_dig_list])
测试
D:\python\python.exe D:/untitled/dir/for.py
1
5
7
zrd
将列表元素倒置显示
c=['cx','zrd','danier','dne','chenxi','ajt','whd','zrd','qwe']
c.reverse()
print(c)
测试
D:\python\python.exe D:/untitled/dir/for.py
['qwe', 'zrd', 'whd', 'ajt', 'chenxi', 'dne', 'danier', 'zrd', 'cx']
Process finished with exit code 0
将列表的打乱的数字元素做排序
x=[5,8,1,3,6,2,7,4]
x.sort()
print(x)
测试
D:\python\python.exe D:/untitled/dir/for.py
[1, 2, 3, 4, 5, 6, 7, 8]
Process finished with exit code 0