Python 列表推导如何工作
在本文中,我们将学习列表推导以及在Python中列表推导的工作原理。
什么是列表推导
使用可以循环遍历的序列或其他列表,列表推导使我们能够快速生成新的列表。在Python中,可迭代对象是指可以循环遍历的任何东西。一种称为列表推导的句法技巧使我们能够通过旧列表构建新列表。列表推导是基于循环(或“for”循环)的。任何列表推导都可以表示为一个for循环,但当它表示为单行的等价列表推导时,它看起来真的很独特。
列表推导 - 众所周知,Python以其简短的语法特性而著名,列表推导使用更短的语法来创建基于现有列表的新列表。
语法
newlist = [ expression(element) for element in oldlist if condition ]
列表推导的优点
- 与循环相比,列表推导更节省空间和时间。
-
需要更少的代码行。
-
将迭代语句转换为公式。
-
在向列表添加项目时,列表推导比for循环快得多。
-
列表推导是内置的map和filter函数的很好替代。
使用列表推导遍历列表的示例
示例
以下程序使用列表推导遍历列表,并创建一个新的列表:
# iterating through the list using loop and list comprehension to create new list
resultList = [element for element in [5, 10, 15, 20, 25]]
# printing the resultant list 1. How do list comprehensions in Python work?
print(resultList)
输出
[5, 10, 15, 20, 25]
使用列表推导式打印奇数
示例
以下程序使用列表推导式打印出20以下的所有奇数 –
# getting all the odd elements in a range of 20(excluded)
# using list comprehension
OddList = [element for element in range(20) if element % 2 != 0]
# printing all the odd numbers below 20
print(OddList)
输出
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
使用列表推导打印矩阵
示例
以下程序使用列表推导打印3*3矩阵−
# creating matrix with 3 rows and 3 columns using list comprehension
inputMatrix = [[n for n in range(3)] for m in range(3)]
# printing the 3x3 matrix
print(inputMatrix)
输出
在执行上述程序后,将生成以下输出结果:
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]
列表解析与for循环的比较
使用for循环
下面的程序使用for循环和append()函数创建了一个包含字符串所有字符的列表。
示例
# creating an empty list for storing resultant string characters
resultList = []
# input string
inputString = 'Tutorialspoint'
# traversing in each character of the
# input string using for loop(in a Traditional way)
for char in inputString:
# appending corresponding string character to the resultant list
resultList.append(char)
# printing the resultant list which is initialized above
print(resultList)
输出
['T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't']
在上面的代码中,我们使用标准方法迭代了一个列表、字符串、元组等。列表推导执行相同的任务,并使程序更易读。
使用列表推导
列表推导用于简化传统的迭代方法。它通过使用for循环转换为一个简单的公式来实现。
示例
以下程序将演示如何使用列表推导创建出与字符串中所有字符相同的列表 –
# iterating through each character in a string
# using list comprehension
resultList = [char for char in 'Tutorialspoint']
# printing the result list containing all the characters
# of a string passed
print(resultList)
输出
['T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't']
嵌套列表推导式
一个列表推导式中嵌套另一个列表推导式被称为嵌套列表推导式
示例
下面的程序展示了嵌套循环的实现方式 –
# creating an empty list for storing the result matrix
outMatrix = []
for m in range(3):
# appending an empty sublist to the above-defined output tMatrix
outMatrix.append([])
for n in range(4):
# appending corresponding element to the output Matrix
outMatrix[m].append(n)
# printing the output matrix
print(outMatrix)
输出
在执行上面的程序时,将生成以下输出结果 −
[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
使用嵌套列表推导实现嵌套循环
通过使用嵌套列表推导,可以用更少的代码行创建相同的结果。
示例
以下程序使用列表推导创建嵌套列表-
# creating an empty list for storing the result matrix
# implementing nested loop using Nested list comprehension
# getting a 3*4 matrix
outMatrix = [[n for n in range(4)] for m in range(3)]
# printing the resultant 3*4 matrix
print(outMatrix)
输出
执行上述程序后,将生成以下输出−
[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
使用列表推导式来展开多维列表
假设我们需要展开一个二维列表。我们可以通过使用列表推导式和子列表来轻松实现这一目标。
示例
下面的程序展示了嵌套循环的实现:
# input 2-dimensional(2D) list
input2DList = [[4, 6, 1], [2, 5, 3], [0, 1, 2]]
# flattening to a one-dimensional list
flattenList = [value for sublist in input2DList for value in sublist]
# printing the flattened list
print(flattenList)
输出
在执行上述程序后,将生成以下输出:
[4, 6, 1, 2, 5, 3, 0, 1, 2]
结论
本文教我们什么是列表生成式以及它们的优势。为了理解列表生成式的工作原理,我们使用了一些示例。我们还对比了几种利用列表生成式来减少代码量的方法。为了和没有使用列表生成式的代码进行对比,我们使用了几个比较样本。