Python入门:Python内置数据结构之列表List

列表的基本概念和特点

列表(list):是一种数据项构成的有限序列,即按照一定的线性顺序,排列而成的数据项的集合,在这种数据结构上进行的基本操作包括对元素的的查找,插入,和删除

  • 一个队列,一个整齐排列的队伍

  • 列表内的个体称作元素,由若干元素组成列表

  • 元素可以是任意对象(数字,字符串,列表,元组等)

  • 列表是线性的数据结构,在内存中连续

  • 使用[]表示

  • 列表是可变的

列表list的定义 初始化

  • list() -> new empty list

  • list(iterable) -> new list initialized from iterable’s items

  • 列表不能一开始就定义大小

lst = list()
lst = []
lst = [2,6,9,'ab']
lst = list(range(5))

以上这几种常用的方法都可以定义一个list

列表的索引访问

  • 索引,也叫下标

  • 正索引:从左至右,从0开始,为列表中每一个元素编号

  • 负索引:从右至左,从-1开始

  • 正负索引都不可以超界,否则引发一场IndexError

  • 为了方便理解,可以认为列表是从左至右排列的,左边是头部,右边是尾部,左边是下界,右边是上界

通过索引访问列表

list[index]

list[index]:index就是索引,使用中括号访问.

  • 索引操作非常简单,时间复杂度为O(1)
lst = list(range(0,100,2))

lst[6]
12

lst[-1]
98

lst[55]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-13-d741eb4099ee> in <module>
----> 1 lst[55]

IndexError: list index out of range


lst[-56]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-14-5e413df323f5> in <module>
----> 1 lst[-56]

IndexError: list index out of range

可见,当输入一个合法的索引时,python能给我们返回一个和索引匹配的值,但是只要索引超界,无论是正索引还是负索引,都会报错IndexError

列表的查询

index方法

  • list.index(value,[start[,stop]])

通过值value,从指定区间寻找列表中的元素是否匹配(当不指定区间时默认搜索整个列表)
匹配到第一个值就停止并返回这个值的索引
匹配不到的话,就报异常ValueError

  • index需要遍历列表,时间复杂度为O(n)

lst = list(range(0,100,2))
lst = list(range(0,100,2))

lst.index(6)
3

lst.index(82,0,48)
41

lst.index(44,-50,-1)
22

lst.index(44,-1,-50)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-923711e149a6> in <module>
----> 1 lst.index(44,-1,-50)

ValueError: 44 is not in list

当我们使用list.index()方法查询列表时,列表中缺少这个value和我们错误地指定区间都有可能导致报错ValueError.
如上面这段代码,指定区间为(-50,-1)时,程序能正常运行,但是当我们改变查找区间为(-1,-50)时,程序报错.在使用负索引指定区间时一定要特别注意区间的方向.

count方法

list.count(object)
  • list.count(object):返回列表中匹配object的次数.
  • count()方法需要遍历列表,时间复杂度为O(n)
lst = list(range(50))

lst.count(2)
1

lst.count(101)
0

当匹配到一个object时,计数器加1.遍历完整个列表,输出匹配到object的次数.一次都没有匹配到时,输出0.

len方法

len(obj)
  • len(obj):返回容器中的项目数.
lst = list(range(5))

len(lst)
50

列表元素的修改

索引访问修改

list[index] = value

  • list[index] = value .修改列表中指定索引下的元素的值
>>> lst = list(range(5))
>>> lst
[0, 1, 2, 3, 4]
>>> lst[0] = 10
>>> lst
[10, 1, 2, 3, 4]
>>> lst[10] = 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

通过指定某个索引可以把这个索引位置修改为我们需要的值.当索引超界时,系统报错IndexError

列表增加,插入元素

list.append(object) -> none

  • list.append(object):在列表尾部增加元素
>>> lst = [1,2,3,4,5]
>>> lst.append(12)
>>> lst
[1, 2, 3, 4, 5, 12]

list.append()方法只能在列表尾部增加,时间复杂度O(1)
返回None意味着没有新的列表产生,就地修改

list.insert()

  • list.insert(index,object) -> None:在指定的索引Index处插入元素object
  • 返回None意味着没有新的列表产生,就地修改
  • 时间复杂度O(n)
>>> lst = [1,2,3,4,5]   #定义一个新列表
>>> lst.insert(1,10)     #在索引1处插入10
>>> lst
[1, 10, 2, 3, 4, 5]
>>> lst.insert(-1,1)      #在索引-1处插入1
>>> lst
[1, 10, 2, 3, 4, 1, 5]
>>> lst.insert(100,666)   #在索引100处插入666
>>> lst
[1, 10, 2, 3, 4, 1, 5, 666]
>>> lst.insert(-100,666)    #在索引-100处,插入666
>>> lst
[666, 1, 10, 2, 3, 4, 1, 5, 666]

可见,当正索引超界时,在尾部追加.负索引超界时,在头部追加.

list.extend()

  • list.extend(iterable) -> None:
    • 将可迭代元素的对象追加进来,返回None
    • 就地修改
>>> lst = [1,1,1]
>>> lst.extend(range(5))
>>> lst
[1, 1, 1, 0, 1, 2, 3, 4]

list1 + list2

  • list1 + list2 -> list:
    • 连接操作,将两个列表连接起来
    • 产生新的列表
    • 本质上是调用的_add_()方法
>>> lst1 = [2,2,2]
>>> lst2 = [1,1,1]
>>> lst1 + lst2
[2, 2, 2, 1, 1, 1]

list * Integer

  • list * Integer -> list:
    • 重复操作,将本列表元素重复n次,返回新的列表
>>> lst = [1,2,3]
>>> lst * 3
# 和正整数相乘
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> lst * 0
# 和0相乘
[]
>>> lst = [1,2,3]
>>> lst * (-1)
# 和负整数相乘
[]
>>> lst * 3.0
# 和浮点数相乘
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'

当列表和浮点数相乘时,系统报错TypeError,不能和非整数做该运算.
当列表和非正整数(包括0和负数)相乘时,得到的新列表为[]列表(空列表).

列表删除元素

remove()

  • list.remove(value) -> None:
    • 从左至右查找第一个匹配value的值,移除该元素
    • 就地修改
>>> lst = [1,3,5,7,9]
>>> lst.remove(5)
>>> lst
[1, 3, 7, 9]
>>> lst.remove(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list

当输入的value在list中无法找到时,程序报错ValueError.
remove方法是先遍历,找到匹配的value值后再删除,效率较低.

list.pop()

  • list.pop([index]) -> item:
    • 不指定索引index,就从列表尾部弹出一个元素,返回该元素.
    • 指定索引index,就从索引处弹出一个元素,索引超界时报错IndexError
>>> lst = [1,2,3,4,5,6,7]
>>> lst.pop(0)
1
>>> lst.pop()
7
>>> lst
[2, 3, 4, 5, 6]

pop方法默认使用从末尾弹出时时间复杂度为O(1),效率很高.
当我们使用pop方法对列表进行操作时,返回的时弹出的元素的值而不是修改后的列表.

list.clear()

  • list.clear() -> None:
    • 清除列表所有元素,剩下一个空列表
>>> lst = [1,2,3,4,5]
>>> lst.clear()
>>> lst
[]

列表其他操作

  • list.reverse()

list.reverse() -> None:
将列表元素反转,返回None
就地修改

>>>lst = [1,2,3,4,5]
>>>lst.reverse()
>>>lst
[5,4,3,2,1]

reverse方法效率不高,尤其是在数据量较大时.

list.sort()

  • sort(key=None,reverse=False) -> None:
    • 对列表元素进行排序,就地修改,默认升序
    • reverse为True,则反转列表,变为降序
    • key一个函数,指定key如何排序
>>> lst
[5, 4, 3, 2, 1]
>>> lst = [3,9,5,1,5,6,23,54,88,2,1]
>>> lst.sort()
>>> lst
[1, 1, 2, 3, 5, 5, 6, 9, 23, 54, 88]
>>> lst.sort(reverse=True)
>>> lst
[88, 54, 23, 9, 6, 5, 5, 3, 2, 1, 1]

sort方法默认升序,reverse=True时变为降序

>>> lst = [3,9,5,1,5,6,23,3.5,2.7,44.0]
>>> lst.sort()
>>> lst
[1, 2.7, 3, 3.5, 5, 5, 6, 9, 23, 44.0]

当出现浮点数时,sort方法可以正确的和整型比较大小.

>>> lst = [3,9,5,1,5,'3','a',0]
>>> lst.sort()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'int'

报错TypeError,<这个比较运算符不支持在str类型和int类型之间使用.我们甚至还可以看到sort的排序方法是使用了比较运算符的

>>> lst = [3,9,5,1,5,'3','a',0]
>>> lst.sort(key=str)
>>> lst
[0, 1, 3, '3', 5, 5, 9, 'a']

这个时候给key传递一个str参数,在进行sort排序时默认使用函数str对列表内的元素进行处理
则成功排序,修改生成了一个排序后的list

object in list

  • object in list:
    • 判断一个对象是否存在于这个元素中,返回bool值
>>> lst = [1,2,[3,4],5,6]
>>> 2 in lst
True
>>> 10 in lst
False
>>> 3 in lst
False
>>> [3,4] in lst
True

当元素存在于这个列表中时,返回True,否则返回False.
值得注意的是,即使某个元素存在于列表中某个容器元素中而不是直接作为列表的子元素,in操作只能返回False.



以上就是我总结的Python3中列表和列表常用操作的一些基础知识,希望通过整理和总结的过程能让我对于知识的了解更为深刻.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值