python--列表学习

列表是什么?

列表是由一系列按特定顺序排列的元素组成。
在Python中,用方括号([])来表示列表,并用逗号来分隔其中的元素。

下面是一个简单的列表示例,这个列表包含几种水果:

>>> fruit = ['cherry', 'peach', 'pear', 'apple']
>>> type(fruit)
<class 'list'>
>>> print(fruit)
['cherry', 'peach', 'pear', 'apple']

访问列表元素

列表是有序集合,因此要访问列表的任何元素,只需将该元素的位置或者索引告诉 Python 即可,索引从 0 而不是 1 开始。

>>> fruit[2]
'pear'

python 为访问最后一个列表元素提供了一种特殊语法。通过将索引指定为-1,可访问最后一个列表元素。

>>> fruit[-1]
'apple'

修改、添加和删除元素

修改列表元素

要修改列表元素,可指定列表名和要修改的元素的索引,再指定该元素的新值。

>>> fruit = ['cherry', 'peach', 'pear', 'apple']
>>> print(fruit)
['cherry', 'peach', 'pear', 'apple']
>>> fruit[2] = 'banana'
>>> print(fruit)
['cherry', 'peach', 'banana', 'apple']

在列表中添加元素

在列表末尾添加元素

使用 append() 可在列表末尾添加元素。

>>> fruit = []
>>> print(fruit)
[]
>> fruit.append('apple')
>>> fruit.append('peach')
>>> fruit.append('banana')
>>> print(fruit)
['apple', 'peach', 'banana']
在列表中添加元素

使用 insert() 可在列表的任何位置添加新元素,需要指定新元素的索引和值。

>>> fruit = ['cherry', 'peach', 'pear', 'apple']
>>> fruit.insert(1, 'banana')
>>> print(fruit)
['cherry', 'banana', 'peach', 'pear', 'apple']

从列表中删除元素

使用 del 语句删除元素

如果知道要删除的元素在列表中的位置,可使用 del 语句。
使用 del 可删除任何位置处的列表元素,条件是知道其索引。

>>> fruit = ['cherry', 'peach', 'pear', 'apple']
>>> del fruit[2]
>>> print(fruit)
['cherry', 'peach', 'apple']
使用方法 pop() 删除元素

方法 pop() 可删除列表末尾的元素,并让你能够接着使用它。

>>> fruit = ['cherry', 'peach', 'pear', 'apple']
>>> my_fruit = fruit.pop()
>>> print(my_fruit)
apple
>>> print(fruit)
['cherry', 'peach', 'pear']
弹出列表中任何位置处的元素

可以通过pop()来删除列表中任何位置的元素,只需在括号中指定要删除的元素的索引即可。

>>> fruit = ['cherry', 'peach', 'pear', 'apple']
>>> my_fruit = fruit.pop(2)
>>> print(my_fruit)
pear
>>> print(fruit)
['cherry', 'peach', 'apple']
根据值删除元素

有时候,你不知道要从列表中删除的值所处的位置。如果你只知道要删除的元素的值,可使用方法remove()。

>>> fruit = ['cherry', 'peach', 'pear', 'apple']
>>> my_fruit = fruit.remove('peach')
>>> print(my_fruit)
None
>>> print(fruit)
['cherry', 'pear', 'apple']

方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来判断是否删除了所样这样的值。

组织列表

使用方法sort()对列表进行永久性排序

>>> fruit = ['cherry', 'peach', 'pear', 'apple']
>>> fruit.sort()
>>> print(fruit)
['apple', 'cherry', 'peach', 'pear']

还可以按与字母顺序相反的顺序排列列表元素,为此,只需向sort()方法传递参数reverse=True即可。

>>> fruit = ['cherry', 'peach', 'pear', 'apple']
>>> fruit.sort(reverse=True)
>>> print(fruit)
['pear', 'peach', 'cherry', 'apple']

使用函数sorted()对列表进行临时排序

>>> fruit = ['cherry', 'peach', 'pear', 'apple']
>>> print(sorted(fruit))
['apple', 'cherry', 'peach', 'pear']
>>> print(fruit)
['cherry', 'peach', 'pear', 'apple']

倒着打印列表

>>> fruit = ['cherry', 'peach', 'pear', 'apple']
>>> fruit.reverse()
>>> print(fruit)
['apple', 'pear', 'peach', 'cherry']
>>> fruit.reverse()
>>> print(fruit)
['cherry', 'peach', 'pear', 'apple']

确定列表的长度

使用len()可快速获取列表的长度。

>>> fruit = ['cherry', 'peach', 'pear', 'apple']
>>> print(len(fruit))
4

列表的使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值