首页 > 编程语言 > 详细

python comprehension

时间:2021-01-10 22:21:30      阅读:25      评论:0      收藏:0      [点我收藏+]

python comprehension

python提供了一种语法,让从已存在的集合上创建集合更加容易的办法,英文称为Comprehension.

List comprehesion are used to map input values to output values, applying a filter along the way to include or exclude any values that meet a specific condition.

Example:

input_strings = ["1", "5", "28", "131", "3"]
# without comprehension
output_integers = []
for num in input_strings:
    output_integers.append(int(num))
print(output_integers)


# with comprehension
output_integers = [int(num) for num in input_strings]
print(output_integers)

# with filter
output_integers = [int(num) for num in input_strings if len(num) < 3]
print(output_integers)

输出:

[1, 5, 28, 131, 3]
[1, 5, 28, 131, 3]
[1, 5, 28, 3]

python comprehension

原文:https://www.cnblogs.com/buzhouke/p/14258591.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!