列表是什么?

列表由一些列按照特定顺序排列而成的元素。

在Python中,由[]表示列表,并用“,”分隔开其中的元素

1
2
bicycle = ['trek','cannondale','redline']
print(bicycle)

这样的话会打印

1
['trek','cannondale','redline']

没错,他会连着【】一块打印下来,没想到吧

访问列表元素

列表式有序集合,所以要访问列表式只需要将该元素的索引告诉Python就好。

例如

1
2
bicycle = ['trek','cannondale','redline']
print(bicycle[0])

当你请求访问列表元素时,Python只会返回该元素,而不包括【】

1
trek

当然你也可以使用之前学习过的方法

1
2
bicycle = ['trek','cannondale','redline']
print(bicycle[0].title())

这样的话会输出

1
Trek

值得一提的是

索引是从0开始而并不是1开始

在Python中,第一个列表元素的索引为0而不是1.(这一点 就跟C语言的数组那块一样,都是从0开始的)

1
2
3
bicycle = ['trek','cannondale','redline']
print(bicycle[1])
print(bicycle[2])
1
2
cannondale
redline

当你想要倒过来访问列表的时候,Python会提供给你一种特殊的语法。通过将索引定位为-1,可让Python返回最后一个列表元素:

1
2
bicycle = ['trek','cannondale','redline']
print(bicycle[-1])
1
redline
修改添加和删除元素
修改

很简单,用索引定位就好

1
2
3
4
bicycle = ['trek','cannondale','redline']
print(bicycle)
bicycle[0] = 'baba'
print(bicycle)
1
2
['trek','cannondale','redline']
['baba','cannondale','redline']
添加

在列表未添加元素

1
.append

这是一个很神奇的东西,他可以让你在列表的末尾添加元素

1
2
3
4
bicycle = ['trek','cannondale','redline']
print(bicycle)
bicycle.append = ('ca')
print(bicycle)
1
2
'trek','cannondale','redline'
'trek','cannondale','redline''ca'

当然,如果你足够闲得无聊,你也来可以尝试

1
2
3
4
5
6
7
name = []
name.append=('zgh')
nmae.append=('cjy')
nmae.append=('czm')
nmae.append=('dyf')
nmae.append=('czh')
print(name)
1
['cgh','cjy','czm','dlf','czh']

有了在末尾添加当然就有在中间添加了

1
2
3
4
bicycle = ['trek','cannondale','redline']
print(bicycle)
bicycle.insect = (1,'ca')
print(bicycle)
1
2
'trek','cannondale','redline'
'trek','ca','cannondale','redline'
删除
1
2
3
4
bicycle = ['trek','cannondale','redline']
print(bicycle)
del bicycle[0]
print(bicycle)
1
2
'trek','cannondale','redline'
'cannondale','redline'

使用del可以无线删除,当然,前提是你知道这个元素在Python中的索引值

当然你也可以使用方法pop()删除

(我感觉这个列表就像一个栈,pop使用就像弹出这个栈顶)

1
2
3
4
5
6
bicycle = ['trek','cannondale','redline']
print(bicycle)
outdate = bicycle.pop()
print(bicycle)
print(outdate)
(这一块本来我打算用popped的,但@一般路过小辉夜给了更好的建议,所以就用outdata了)
1
2
3
'trek','cannondale','redline'
‘trek’'cannondale'
'redline'
弹出任何位置的元素

其实吧,pop这个东西不知能删除最后一个元素,你只要在括号里面填写你想要的元素的索引,他都能给你弹出来

1
2
3
4
5
bicycle = ['trek','cannondale','redline']
print(bicycle)
outdate = bicycle.pop(1)
print(bicycle)
print(outdate)
1
2
3
'trek','cannondale','redline'
'trek,'redline'
'cannondale'

需要注意的是,当你弹出元素的时候,这个元素就不在列表中了

根据特定的值删除

1
2
3
4
bicycle = ['trek','cannondale','redline']
print(bicycle)
bicycle.remove('cannondale')
print(bicycle)
1
2
['trek','cannondale','redline']
['trek',redline']

使用remove的时候也可以照常使用这个值

1
2
3
4
5
bicycle = ['trek','cannondale','redline']
print(bicycle)
abb = 'cannondale'
bicycle.remove('cannondale')
print(f"{abb.title()}")
1
2
['trek', 'cannondale', 'redline']
Cannondale
组织列表

sort()

使用sort()可以将列表进行用就行排序(这个是按照字母进行排序的)
1
2
3
bicycle = ['trek','cannondale','redline']
bicycle.sort()
print(bicycle)
1
['cannondale','redline','trek']

当你要逆字母方向时,需要

1
2
3
bicycle = ['trek','cannondale','redline']
bicycle.sort(reverse = True)
print(bicycle)
1
['trek', 'redline', 'cannondale']
使用sorted函数进行临时性的排序
1
2
3
4
bicycle = ['trek','cannondale','redline']
print(bicycle)
print(sorted(bicycle))
print(bicycle)
1
2
3
['trek', 'cannondale', 'redline']
['cannondale', 'redline', 'trek']
['trek', 'cannondale', 'redline']
倒着打印列表
1
2
3
4
bicycle = ['trek','cannondale','redline']
print(bicycle)
bicycle.reverse()
print(bicycle)
1
2
['trek', 'cannondale', 'redline']
['redline', 'cannondale', 'trek']
确定列表长度
1
2
3
>>>bicycle = ['trek','cannondale','redline']
>>>len(bicycle)
3