Python 程序设计入门(008)—— 列表的操作(3):列表推导式

使用列表推导式可以快速生成一个列表。或者根据某个列表生成满足指定需求的列表。列表推导式包含在方括号内,它由一个表达式、一个或多个 for 循环和一个用于过滤生成的列表的可选条件组成。

列表推导式的语法格式如下:

new_list = [expression(item) for item in iterable if condition]

相当于如下语句:
new_list = []
for item in iterable:
    if condition:
        new_list.append(expression(item))

说明:

(1)Expressinon:表达式,用于计算列表的元素。

(2)item:循环变量。

(3)iterable:可迭代对象。

(4)condition:条件。

一、基本应用举例

1、生成 1 到 10 组成的列表

num1 = [x for x in range(1,11)]
print(num1)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

上面的例子表示:按照 int 类型的长度分配两块连续区域,强制转换为 int 类型的指针,并把首地址赋予指针变量 ps。

2、生成 2-20 之间的偶数组成的列表

num1 = [2*x for x in range(1,11)]
print(num1)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

3、生成 1-19 之间的奇数组成的列表

num1 = [2*x-1 for x in range(1,11)]
print(num1)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

4、生成嵌套列表

num1 = [[x,x**2,x**3] for x in range(1,11)]
print(num1)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
[[1, 1, 1], [2, 4, 8], [3, 9, 27], [4, 16, 64], [5, 25, 125], [6, 36, 216], [7, 49, 343], [8, 64, 512], [9, 81, 729], [10, 100, 1000]]

二、添加条件

1、生成 10 偶数组成的列表

num1 = [x for x in range(1,11) if x%2==0]
print(num1)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
[2, 4, 6, 8, 10]

2、生成 10 奇数组成的列表

num1 = [x for x in range(1,11) if x%2==1]
print(num1)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
[1, 3, 5, 7, 9]

3、生成 1-3 与 7-10 之间的整数组成的列表

num1 = [x for x in range(1,11) if x>=7 or x<=3]
print(num1)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
[1, 2, 3, 7, 8, 9, 10]

4、如果 x 的值介于 2 和 5 之间,则列表推导式返回 2*x,否则返回 x**2

num1 = [2*x if x>=2 and x<=5 else x**2 for x in range(1,11)]
print(num1)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
[1, 4, 6, 8, 10, 36, 49, 64, 81, 100]

三、使用 zip() 函数组合多个列表

与 zip 函数一起使用的列表推导返回一个元组列表,其中第 n 个元组包含每个列表的第 n 个元素。

例1:

cities = ['Rome', 'Warsaw', 'London']
countries = ['Italy', 'Poland', 'United Kingdom']
list1 = [(city, country) for city, country in zip(cities, countries)]
print(list1)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
[('Rome', 'Italy'), ('Warsaw', 'Poland'), ('London', 'United Kingdom')]

例2:

a = [2, 3, 4, 5]
b = [12, 13, 14, 15]
list1 = [x+y for x,y in zip(a,b)]
print(list1)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
[14, 16, 18, 20]

例3:

a = [2, 3, 4, 5]
b = [12, 13, 14, 15]
c = [22, 23, 24, 25]
list1 = [2*x+3*y+z for x,y,z in zip(a,b,c)]
print(list1)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
[62, 68, 74, 80]

四、嵌套列表推导式

list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
list2 = [[x*2 for x in row] for row in list1]
print(list2)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
[[2, 4, 6], [8, 10, 12], [14, 16, 18]]
08-06 18:01