在Python中,列表(Lists)是很常见的数据结构。今天我们要来在线学习列表的相关知识点,主要内容有列表常用操作方法、堆栈、队列、列表推导、列表内置推导以及del表达式。一起来看看吧~
1、列表常用操作方法
list.append(x)
list末尾追加元素 x,等价于 a[len(a):]=[x]
list.extend(iterable)
list末尾追加可迭代类型元素(如添加[1,2])等价于 a[len(a):]=iterable
list.insert(i,x)
list指定位置i添加元素x
list.remove(x)
list中删除元素
list.pop([i])
从指定位置[i]处删除元素,未指定位置时,默认从末尾元素删除。
list.clear()
清空list数据
list.index(x[, start[, end]])
返回x在list中首次出现的位置, start,end 指定查找的范围。
list.count(x)
返回x在list中的个数
list.sort(key=None, reverse=False)
对list进行排序
list.reverse()
list元素倒序
list.copy()
返回list的复制数据,等价于a[:]
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
>>> fruits.count('apple')
2
>>> fruits.count('tangerine')
0
>>> fruits.index('banana')
3
>>> fruits.index('banana', 4) # Find next banana starting a position 4
6
>>> fruits.reverse()
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
>>> fruits.append('grape')
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
>>> fruits.sort()
>>> fruits
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
>>> fruits.pop()
'pear'
2、列表作为堆栈
堆栈的原则是数据 先进后出
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]
3、列表作为队列
队列的原则是数据 先进先出
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry") # Terry arrives
>>> queue.append("Graham") # Graham arrives
>>> queue.popleft() # The first to arrive now leaves
'Eric'
>>> queue.popleft() # The second to arrive now leaves
'John'
>>> queue # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])
4、列表推导
根据List提供的相关方法,我们可以自己根据需求创建list
>>> squares = []
>>> for x in range(10):
... squares.append(x**2)
...
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
也可以使用lambda表达式来创建
squares = list(map(lambda x: x**2, range(10)))
或者更直接
squares = [x**2 for x in range(10)]
使用多个for循环或者if 组合语句也可以创建list
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y][(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
等价于
>>> combs = []
>>> for x in [1,2,3]:
... for y in [3,1,4]:
... if x != y:
... combs.append((x, y))
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
如果表达式为元组类型,使用时必须用 ()
>>> vec = [-4, -2, 0, 2, 4]
>>> # create a new list with the values doubled
>>> [x*2 for x in vec]
[-8, -4, 0, 4, 8]
>>> # filter the list to exclude negative numbers
>>> [x for x in vec if x >= 0]
[0, 2, 4]
>>> # apply a function to all the elements
>>> [abs(x) for x in vec]
[4, 2, 0, 2, 4]
>>> # call a method on each element
>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'loganberry', 'passion fruit']
>>> # create a list of 2-tuples like (number, square)
>>> [(x, x**2) for x in range(6)]
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
>>> # the tuple must be parenthesized, otherwise an error is raised
>>> [x, x**2 for x in range(6)]
File "<stdin>", line 1, in <module>
[x, x**2 for x in range(6)]
^
SyntaxError: invalid syntax
>>> # flatten a list using a listcomp with two 'for'
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
lists推导也可以用混合表达式和内置函数
>>> from math import pi
>>> [str(round(pi, i)) for i in range(1, 6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']
5、列表内置推导
下面是一个 3* 4的矩阵
>>> matrix = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... ]
下面方法可以将matrix数据转换为 4*3的矩阵。
>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
等价于
>>> transposed = []
>>> for i in range(4):
... transposed.append([row[i] for row in matrix])
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
或者是这样
>>> transposed = []
>>> for i in range(4):
... # the following 3 lines implement the nested listcomp
... transposed_row = []
... for row in matrix:
... transposed_row.append(row[i])
... transposed.append(transposed_row)
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
上面操作完全可以使用list的内置函数 zip() 来实现
>>> list(zip(*matrix))
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
6、del表达式
使用可以直接删除List中的某个数值
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]
也可以删除整个 list
>>> del a
Pyhton数据结构列表的学习内容就分享到这里了,相信大家对于Pyhton还有很多的疑惑,现在就在博学谷报名在线学习Python,就有名师一对一对你进行辅导。还等什么,现在就来体验吧~
— 申请免费试学名额 —
在职想转行提升,担心学不会?根据个人情况规划学习路线,闯关式自适应学习模式保证学习效果
讲师一对一辅导,在线答疑解惑,指导就业!
相关推荐 更多
Python知识点详解:UDP和TCP协议的介绍
UDP和TCP都是传输层协议,不过却又一些不同。TVP提供IP环境下的数据可靠传输,它是实现为所发送的数据凯皮出连接的通道,然后再进行数据的发送。而UDP并不为IP提供可靠性,流控或差错回复功能。UDP和TCP到底如何定义?应用场景是如何的呢?下面小编就详细为大家解析一下。
3333
2019-07-11 17:18:53
每天自学Python一小时多久可以掌握?自学Python的常见错误方式
Python作为目前当下最热门的编程语言,吸引了越来越多的人学习,其中不乏有一些零基础的小白,希望通过自学Python以后能有好的工作。不少人还会问,每天自学python一小时多久可以掌握?虽然滴水穿石非一日之功,但是不得不承认,有些人即使努力学习一辈子也比不上别人一年的学习效果。因此单纯的学习时间累积并不能带来好的结果,现在小编带大家看看那些自学Python的常见错误方式,你中了吗?
4361
2019-07-15 19:06:05
Python入门视频看哪个好?适合初学者的教学视频推荐
Python作为一门新手友好的编程语言,对于初学者来说,还是有一定的学习难度的。目前的Python学习资料在网上可以找到很多,那么Python入门视频看哪个好呢?本文就为大家推荐博学谷的免费Python入门教学视频—《从0开始学Python》,即使是初学者学完,也可以快速入门Python。
3216
2019-09-15 16:49:11
五种方法教你Python字符串连接
字符串是Python中最常用的数据类型,在开发过程中可以对字符创进行截取并与其他字符创进行连接。下面小编整理了5种方法完成Python字符创的连接!
1758
2019-12-10 18:39:16
Python应用场景多不多?
Python应用场景多不多?Python应用在网络Web应用发展、用于操作系统管理、服务器维护的自动化脚本、科技计算、电脑软件、服务软体(网路软体)、游戏、设想实现、产品早期原型和迭代等方面。
776
2020-07-06 14:49:03
Python函数入门教程
免费 基础 932
Python高级语法
¥99 进阶 37
Tableau Public 数据可视化项目实战
¥199 基础 139
Python职业规划公开课
免费 基础 2153
零基础Linux入门教程
免费 基础 2624
推荐课程
热门文章
- UI设计培训费用要多少钱?靠不靠谱?
- 哪个Python培训机构好些?怎么选?
- 这样的Java自学姿势 学废最快
- 2021年大数据行业发展前景及岗位方向如何?
- Web前端开发工程师培训班哪家好?
- 互联网产品经理岗位现在有多热门?
- 前端测试用例怎么写?为什么写测试用例?
- 有哪些好的线上培训产品经理的机构?
- 零经验的人学编程难吗?能学会吗?
- 传智博学谷神经网络和深度学习课程推荐 查看更多
扫描二维码,了解更多信息
