当前位置: 首页 > news >正文

网站开发人员选项网站建设首先

网站开发人员选项,网站建设首先,大型农村电商平台,哈尔滨网站制作前景Python list comprehension {列表推导式 - 列表解析式 - 列表生成式} 1. Python list comprehension (列表推导式 - 列表解析式 - 列表生成式)2. Example3. ExampleReferences Python 中的列表解析式并不是用来解决全新的问题,只是为解决已有问题提供新的语法。 列…

Python list comprehension {列表推导式 - 列表解析式 - 列表生成式}

  • 1. Python list comprehension (列表推导式 - 列表解析式 - 列表生成式)
  • 2. Example
  • 3. Example
  • References

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

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

1. Python 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_functionlist_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

列表解析让你只需编写一行代码就能生成列表。列表解析将 for 循环和创建新元素的代码合并成一行,并自动附加新元素。

使用列表解析创建平方数列表:

#!/usr/bin/env python3
# -*- coding:utf-8 -*-squares = [value ** 2 for value in range(1, 11)]print(squares)

要使用这种语法,首先指定一个描述性的列表名,例如 squares。然后,指定一个左方括号,并定义一个表达式,用于生成你要存储到列表中的值。表达式为 value ** 2,它计算平方值。接下来,编写一个 for 循环,用于给表达式提供值,再加上右方括号。在这个示例中,for 循环为 for value in range(1, 11),它将值 1 ~ 10 提供给表达式 value ** 2

这里的 for 语句末尾没有冒号。

/usr/bin/python3.5 /home/strong/workspace/master.py
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]Process finished with exit code 0

在这里插入图片描述

The if statement in list comprehension is optional. For every item in list, execute the expression if the condition is True.

[expression for item in list if condition == True]

2. Example

#!/usr/bin/env python
# -*- coding: utf-8 -*-from __future__ import absolute_import
from __future__ import division
from __future__ import print_functionlist_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_functionkeypoints = 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

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] (美) Eric Matthes (埃里克•马瑟斯) 著, 袁国忠 译. Python 编程:从入门到实践[M]. 北京:人民邮电出版社, 2016. 1-459

http://www.yayakq.cn/news/902888/

相关文章:

  • 免费领取手机网站建立一个自己的网站需要多少钱
  • 大淘客官网做的网站打不开企业公司网站建设公司
  • 关于加强网站建设的情况说明上海网站建设 数字展厅
  • 网站优化吧o2o平台搭建
  • 国外网站内容去哪些平台做合肥网站建设哪里好
  • 中英文双语企业网站网站正在建设中的代码
  • 福州网站建设招商南京软件外包
  • 电脑怎么用别人的网站吗discuz做影视网站
  • 网站正能量晚上免费软件广东省 网站制作
  • 厦门自主建站模板江苏定制网站建设费用
  • 广州网站建设优化方案咋制作网站
  • 徐州市专业做网站的公司网站建设流程百科
  • 医疗器械网站建设策划书长沙制作公园仿竹围栏报价
  • 泰州模板自助建站网店代运营公司哪家好
  • 新乡 网站运营亚马逊被曝将裁员1万人
  • 做网站可以用什么主题国内十大游戏公司排名
  • 网站功能简介二手车网站开发背景
  • 网站收录查询接口深圳网站建设公司哪好
  • 网站排名软件利搜怎么样网站建设项目成本估算表
  • 湖南网站建设小公司排名做网站的怎么挣钱、
  • 可以做水果的团购网站有哪些如何网站专题策划
  • 设计网站技术广西南宁建设厅网站首页
  • 宁波网站搜索优化做一套二级域名网站怎么做
  • php美食网站开发背景怎么选择扬中网站建设
  • 网站查询备案服务商南京黑马程序员培训学校
  • 小说网站开发 公司网页设计免费模板中文
  • 怎样从用户体现提高网站的搜索引擎信任度外贸网站建设费用情况
  • 做网站导出用什么色彩模式岳阳企业网站建设
  • 百度搜索官方网站国外vps
  • 一个人做网站可以做什么html网站源代码