List Comprehension - 列表推导式 - 列表解析式 - 列表生成式

List Comprehension - 列表推导式 - 列表解析式 - 列表生成式

Python 中的列表解析式并不是用来解决全新的问题,只是为解决已有问题提供新的语法。

列表推导式的优点是相比于 for 循环更高效,因为列表推导式在执行时调用的是 Python 的底层 C 代码,而 for 循环则是用 Python 代码来执行。

1. List Comprehension - 列表推导式 - 列表解析式 - 列表生成式

在这里插入图片描述

列表推导式的语法结构:

  • [] 定义列表的中括号。
  • for 循环初步定义列表。
  • (可选) 在 for 循环后面可以使用 if 语句进行过滤。
  • 在for循环前的是列表的元素表达式,可以是任意的表达式。可以是 for 循环中的元素本身,可以是元素进行运算后的结果,可以是元素组成的元组或者列表,可以是一个函数,甚至可以是另一个列表解析式 (嵌套列表解析式)。
  • (可选) 在for循环后面可以再嵌套for循环。
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

list_data = [i for i in range(10)]
square_data = [i ** 2 for i in range(10)]

print(list_data)
print(square_data)
/usr/bin/python3.5 /home/strong/sunergy_moonergy_work/yongqiang.py
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Process finished with exit code 0

2. Example

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

list_data_1 = list(range(1, 11))
list_data_2 = list(range(10))

square_data = [i * i for i in range(10)]
list_comprehension_a = [i * 2 for i in list_data_2]
list_comprehension_b = [x for x in list_data_2 if x >= 5]
list_comprehension_c = [(x, x * 2) for x in range(10)]
list_comprehension_d = [[x, x * 2] for x in range(10)]

matrix_data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
list_comprehension_e = [num for row in matrix_data for num in row]

list_comprehension_f = [n for n in list_data_2 if n % 2 == 1]

print("list_data_1:", list_data_1)
print("list_data_2:", list_data_2)
print("square_data:", square_data)
print("list_comprehension_a:", list_comprehension_a)
print("list_comprehension_b:", list_comprehension_b)
print("list_comprehension_c:", list_comprehension_c)
print("list_comprehension_d:", list_comprehension_d)
print("list_comprehension_e:", list_comprehension_e)
print("list_comprehension_f:", list_comprehension_f)
/usr/bin/python3.5 /home/strong/sunergy_moonergy_work/yongqiang.py
list_data_1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list_data_2: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
square_data: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
list_comprehension_a: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
list_comprehension_b: [5, 6, 7, 8, 9]
list_comprehension_c: [(0, 0), (1, 2), (2, 4), (3, 6), (4, 8), (5, 10), (6, 12), (7, 14), (8, 16), (9, 18)]
list_comprehension_d: [[0, 0], [1, 2], [2, 4], [3, 6], [4, 8], [5, 10], [6, 12], [7, 14], [8, 16], [9, 18]]
list_comprehension_e: [1, 2, 3, 4, 5, 6, 7, 8, 9]
list_comprehension_f: [1, 3, 5, 7, 9]

Process finished with exit code 0

3. Example

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

keypoints = list(range(1, 76))
list_comprehension_keypoints = [keypoints[i:i + 3] for i in range(0, len(keypoints), 3)]

print("keypoints:", keypoints)
print("list_comprehension_keypoints:", list_comprehension_keypoints)
/usr/bin/python3.5 /home/strong/sunergy_moonergy_work/yongqiang.py
keypoints: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75]
list_comprehension_keypoints: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 23, 24], [25, 26, 27], [28, 29, 30], [31, 32, 33], [34, 35, 36], [37, 38, 39], [40, 41, 42], [43, 44, 45], [46, 47, 48], [49, 50, 51], [52, 53, 54], [55, 56, 57], [58, 59, 60], [61, 62, 63], [64, 65, 66], [67, 68, 69], [70, 71, 72], [73, 74, 75]]

Process finished with exit code 0
发布了509 篇原创文章 · 获赞 1824 · 访问量 110万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104681757
今日推荐