在线客服
扫描二维码
下载博学谷APP扫描二维码
关注博学谷微信公众号
在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,就有名师一对一对你进行辅导。还等什么,现在就来体验吧~
— 申请免费试学名额 —
在职想转行提升,担心学不会?根据个人情况规划学习路线,闯关式自适应学习模式保证学习效果
讲师一对一辅导,在线答疑解惑,指导就业!
相关推荐 更多
0基础Python数据分析入门学习
对于0基础的同学来说,想要学习Python,数据分析是必须掌握的一个知识要点。本文就专门针对0基础的同学,整理了数据分析入门的基础知识点,分别从是什么,为什么,有什么用三大问题着手带大家了解数据分析。
4650
2019-08-02 11:19:23
Python入门视频看哪个好?适合初学者的教学视频推荐
Python作为一门新手友好的编程语言,对于初学者来说,还是有一定的学习难度的。目前的Python学习资料在网上可以找到很多,那么Python入门视频看哪个好呢?本文就为大家推荐博学谷的免费Python入门教学视频—《从0开始学Python》,即使是初学者学完,也可以快速入门Python。
6595
2019-09-15 16:49:11
分享15天学习python视频教程?隔壁的小伙伴都羡慕了
学习python之前要理清楚视频目录大纲,配合自己的时间,结合视频大纲学习,本套教程15天学前环境搭建,1-3 天内容为Linux、基础命令4-13 天内容为Python基础教程、14-15天内容为飞机大战项目演练。合理安排学习时间,python越上手。
2827
2020-01-13 15:56:09
Python运算符总结
所有的编程语言本质就是在解决运算逻辑,通过各种算法实现想要的各种功能,因此在学习Python编程语言时,不仅要掌握各种变量类型,深刻理解函数式编程的原理,还要彻底搞懂各类运算符的使用。通过本片文章你可以了解到在Python编程开发中的各类运算符以及其使用方法。
3563
2020-06-08 16:31:37
为什么说Python入门容易精通难?
Python应用领域广泛,没有具体的学习方向学完基础知识之后会很迷茫,应先确定好学习方向,按照这个学习方向建立学习路径规划。Python 非常适合初学者上手,相比较于其他主流编程语言其语言环境更接近自然语言,具备良好的可读性。
2370
2021-04-01 16:57:38