原作者:Nik Piepenbreier
翻译&内容补充:乾峰
原文地址: https://towardsdatascience.com/advanced-python-list-techniques-c6195fa699a3
列表(List)是你使用 Python 过程中接触最为频繁的数据结构,也是功能最为强大的几种数据结构之一。 Python 列表非常的万能且蕴含着许多隐藏技巧,下面我们就来探索一些常用的列表技巧。
1 列表元素的过滤
1.1 filter()的使用
filter() 函数接受 2 个参数:1 个函数对象以及 1 个可迭代的对象,接下来我们定义 1 个函数然后对 1 个列表进行过滤。
首先我们创建 1 个列表,并且剔除掉小于等于 3 的元素:
>>> o_list = [1,2,3,4,5]
>>> def filter_three(number):
return number > 3
>>> filter = filter(filter_three, original_list)
>>> filter_list = list(filter)
>>> filter_list
[4, 5]
回顾一下发生了什么:
1. 我们定义了列表 o_list
2. 接着我们定义了一个接受数值型参数 number 的函数 filter_three ,当传入的参数值大于 3 时会返回 True,反之则会返回 False
3. 我们定义了 filter 对象 filtered ,其中 filter() 接受的第一个参数是函数对象,第二个参数是列表对象
4. 最终我们将 filter 对象转化为列表,最终得到经 filter_three 过滤后 original_list 内留下的元素。
1.2 使用列表推导式
类似的,我们也可以利用列表推导式来过滤列表元素,作为一种生成和修改列表优雅的方式,列表推导式想必大家都比较熟悉了,下面是使用列表推导完成同样任务的过程:
>>> o_list = [1,2,3,4,5]
>>> filter_list = list(number for number in o_list if number > 3)
>>> filter_list
[4, 5]
2 修改列表
2.1 map()的使用
Python 中内置的 map() 函数使得我们可以将某个函数应用到可迭代对象内每一个元素之上。
比方说我们想获取到一个列表对象中每一个元素的平方,就可以使用到 map() 函数,就像下面的例子一样:
>>> o_list = [1,2,3,4,5]
>>> def square(number):
return number*2
>>> squ = map(square,o_list)
>>> squ_l = list(squ)
[2,4,6,8,10]
类似 filter() 的工作过程,下面我们来看看发生了什么:
1. 首先我们定义了列表 o_list ,以及接受数值型参数并返回其平方值的函数 square()
2. 接着我们定义了 map 对象 squ,类似filter(),map()接受的第一个参数是函数对象,第二个参数是列表对象
3. 最终我们将map对象squares` 列表化,就得到了想要的结果
2.2 使用列表推导式
同样的我们也可以使用列表推导式完成同样的任务:
>>> o_list = [1,2,3,4,5]
>>> s = list(num for num in o_list num * 2)
>>> s
[2,4,6,8,10]
3 利用 zip()来组合列表
有些情况下我们需要将两个或以上数量的列表组合在一起,这类需求使用 zip() 来完成非常方便。
zip() 函数接收多个列表作为参数传入,进而得到每个位置上一一对应的元素组合,就像下面的例子一样:
>>> numbers = [1,2,3]
>>> letters = ['a', 'b', 'c']
>>> combined = zip(numbers, letters)
>>> combined_list = list(combined)
[(1, 'a'), (2, 'b'), (3, 'c')]
4 颠倒列表
Python 中的列表是有序的数据结构,正因如此,列表中元素的顺序很重要,有些时候我们需要翻转列表中所有元素的顺序,可以通过 Python 中的切片操作,用 ::-1 来快捷地实现:
>>> original_list = [1,2,3,4,5]
>>> reversed_list = original_list[::-1]
>>> print(reversed_list)
5 检查列表中元素的存在情况
有些情况下我们想要检查列表中是否存在某个元素,这种时候就可以使用到 Python 中的 in 运算符,譬如说我们有一个记录了所有比赛获胜队伍名称的列表,当我们想查询某个队名是否已获胜时,可以像下面的例子一样:
>>> games = ['Yankees', 'Yankees', 'Cubs', 'Blue Jays', 'Giants']
>>> def isin(item, list_name):
if item in list_name: print(f"{item} is in the list!")
else: print(f"{item} is not in the list!")
>>> isin('Blue Jays', games)
Blue Jays is in the list!
>>> isin('Angels', games)
Angels is not in the list!
6 找出列表中出现次数最多的元素
有些情况下我们想要找出列表中出现次数最多的元素,譬如对记录若干次抛硬币结果的列表,找出哪一种结果出现次数最多,就可以参考下面的例子:
>>> games = ['heads', 'heads', 'tails', 'heads', 'tails']
>>> items = set(games)
>>> print(max(items, key = games.count))
7 展平嵌套列表
有些情况下我们会遇到一些嵌套的列表,其每个元素又是各自不同的列表,这种时候我们就可以利用列表推导式来把这种嵌套列表展平,如下面 2 层嵌套的例子:
>>> nested_list = [[1,2,3],[4,5,6],[7,8,9]]
>>> flat_list = [i for j in nested_list for i in j]
>>> print(flat_list)
Returns [1, 2, 3, 4, 5, 6, 7, 8, 9]
额外补充:如果是更多层嵌套,就需要有多少层写多少 for 循环,比较麻烦,其实还有一种更好的方法,我们可以使用
pip install dm-tree来安装tree这个专门用于展平嵌套结构的库,可以展平任意层嵌套列表,使用例子如下:
>>> import tree
>>> nested_list_2d = [[1,2,3],[4,5,6],[7,8,9]]
>>> nested_list_3d = [[1,2],[3,4],[5,6]]
>>> print(tree.flattern(nested_list_2d))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(tree.flattern(nested_list_2d))
[1, 2, 3, 4, 5, 6]
8 检查唯一性
如果你想要查看列表中的值是否都是唯一值,可以使用 Python 中的 set 数据结构的特点,譬如下面的例子:
>>> list1 = [1,2,3,4,5]
>>> list2 = [1,1,2,3,4]
>>> def isunique(list):
if len(list) == len(set(list)):
print('Unique!')
else: print('Not Unique!')
>>> isunique(list1)
Unique!
>>> isunique(list2)
Not Unique