Python 笔记 - list(一)

  1. 定义:一系列按特定顺序排列的元素
    【注】不同于C等其他大部分编程语言,Python 不要求列表中各元素类型相同,下面例子中的列表都是合法的:
numbers = [1, 2, 4, 8]
letters = ['a', 'b', 'c']
fruits = ['strawberry', 'banana', 'pear']
## 列表的元素也可以是列表
everytypeisok = [4, 'y', 'Mary', ['drop', 16]]
  1. 访问列表元素
>>> print(fruits)
['strawberry', 'banana', 'pear']
>>> print(fruits[0])
'strawberry'
>>> print(fruits[-1])
'pear'	

和其他大部分编程语言一样,使用下标访问元素时,下标从 0 开始。
此外,Python 允许通过将索引设置为负数,来访问倒数第 i 个元素。

  1. 列表元素的增删改
    1)修改:
>>> fruits[1] = 'raspberry'
>>> print(fruit)
['strawberry', 'raspberry', 'pear']

2)增加:
a. 末尾追加:
使用 append() 方法追加单个元素

>>> fruits.append('mulberry')
>>> print(fruits)
['strawberry', 'raspberry', 'pear', 'mulberry']

使用 extend() 方法向列表1追加列表2中所有元素

>>> fruits2 = ['apple', 'grape']
>>> fruits2.extend(fruits)
>>> print(fruits2)
['strawberry', 'raspberry', 'pear', 'mulberry', 'apple', 'grape']

b. 指定位置插入:

>>> fruits.insert(1, 'grape')
>>> print(fruits)
['strawberry', 'grape', 'raspberry', 'pear', 'mulberry']

3)删除:
a. 使用 del 语句:

>>> del fruits[3]
>>> print(fruits)
['strawberry', 'grape', 'raspberry', 'mulberry']

b. 使用 pop() 方法删除列表元素,并返回它的值:

>>> popped_fruit = fruits.pop()
>>> print(fruits)
['strawberry', 'grape', 'raspberry', 'mulberry']
>>> print(popped_fruit)
'mulberry'

pop() 方法默认删除列表末尾元素,若指定了索引值,则删除索引位置对应的元素:

>>> fruit = fruits.pop(1)
>>> print(fruits)
['strawberry', 'raspberry', 'mulberry']
>>> print(fruit)
'grape'

c. 使用 remove() 方法删除指定值元素:

>>> print(fruits)
['strawberry', 'raspberry', 'mulberry']
>>> fruits.remove('raspberry')
>>> print(fruits)
['strawberry', 'mulberry']

若列表中存在两个或以上相同的值,删除索引位置小的那个元素,如果需要全部删除,需要使用循环来实现。
d. 使用 clear() 方法清空列表:

>>> fruits.clear()
>>> print(fruits)
[]

使用 del 语句同样可以清空列表

>>> fruits = ['strawberry', 'raspberry', 'mulberry']
>>> print(fruits)
['strawberry', 'raspberry', 'mulberry']
>>> del fruits[:]
>>> print(fruits)
[]
  1. 排序:
    a. 使用 sort() 方法对列表进行永久性排序:
>>> fruits = ['strawberry', 'raspberry', 'mulberry', 'pear', 'banana']
>>> fruits.sort()
>>> print(fruits)
['banana', 'mulberry', 'pear', 'raspberry', 'strawberry']

通过设置参数 reverse=True 进行反序排序

>>> fruits = ['strawberry', 'raspberry', 'mulberry', 'pear', 'banana']
>>> fruits.sort(reverse = True)
>>> print(fruits)
['strawberry', 'raspberry', 'pear', 'mulberry', 'banana']

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

>>> fruits = ['strawberry', 'raspberry', 'mulberry', 'pear', 'banana']
>>> print(sorted(fruits))
['strawberry', 'raspberry', 'pear', 'mulberry', 'banana']

sorted() 函数同样支持反序排序:

>>> print(sorted(fruits, key = None, reverse = True))
['strawberry', 'raspberry', 'pear', 'mulberry', 'banana']

c. 使用 reverse() 方法对列表进行永久性逆序:

>>> fruits = ['strawberry', 'raspberry', 'mulberry', 'pear', 'banana']
>>> fruits.reverse()
>>> print(fruits)
['banana', 'pear', 'mulberry', 'raspberry', 'strawberry']
>>> 

【注】排序方法并不适用所有列表,例如:

>>> listofdifferenttypes = [1, 'hello world', [1, 2, 4, 8]]
>>> listofdifferenttypes.sort()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'int'
  1. 使用 len() 方法获取列表长度:
>>> print(len(fruits))
5
  1. 使用 index() 方法获取指定值所在位置的索引
>>> print(fruits.index('pear'))
1
  1. 使用 count() 方法计算指定值在列表中出现的次数:
>>> fruits = ['strawberry', 'raspberry', 'mulberry', 'raspberry']
>>> print(fruits.count('strawberry'))
1
>>> print(fruits.count('raspberry'))
2
  1. 使用 copy() 方法对列表进行拷贝:
>>> copied_fruits = fruits.copy()
>>> print(copied_fruits)
['strawberry', 'raspberry', 'mulberry', 'raspberry']

下面赋值语句同样支持列表拷贝操作:

>>> copied_fruits = fruits[:]
>>> copied_fruits
['strawberry', 'raspberry', 'mulberry', 'raspberry']
>>> copied_fruits = fruits[1:3]
>>> copied_fruits
['raspberry', 'mulberry']
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值