python13解析式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python13解析式相关的知识,希望对你有一定的参考价值。
1、解析式是什么?为什么要用解析式?
对一个list里所有的数值求平方
In [123]: lst = list(range(6)) In [124]: ret = [] In [125]: for i in lst: ...: ret.append(i ** 2) ...: In [126]: ret Out[126]: [0, 1, 4, 9, 16, 25]
解析式:
In [127]: lst = list(range(6)) In [128]: [ x ** 2 for x in lst ] # 列表解析 Out[128]: [0, 1, 4, 9, 16, 25]
求list中偶数的平方
In [137]: lst Out[137]: [0, 1, 2, 3, 4, 5] In [138]: for i in lst: ...: if i % 2 == 0: ...: print(i ** 2) ...: ...: 0 4 16 In [140]: [ i ** 2 for i in lst if i % 2 == 0 ] Out[140]: [0, 4, 16]
列表解析的语法格式:
[ expr for i in iterable ]
[ expr for i in iterable if子句 ]
解析式代码简洁,可读性高;效率比普通迭代稍高
if子句可以出现多次:
In [143]: [ i ** 2 for i in lst if i % 2 == 0 if i > 0] Out[143]: [4, 16] In [151]: [ i ** 2 for i in lst if i % 2 == 0 and i > 0 and i < 4 ] Out[151]: [4] In [149]: [ i ** 2 for i in lst if i % 2 == 0 if i > 0 or i < 4 ] Out[149]: [0, 4, 16]
带多个if语句的都可以转化为条件的逻辑运算,所以一般不会带多个if语句
for语句也可以有多个: # 就是嵌套迭代
In [153]: [ x, y for x in range(5) for y in range(5, 10) ] File "<ipython-input-153-8aadadd13ebf>", line 1 [ x, y for x in range(5) for y in range(5, 10) ] ^ SyntaxError: invalid syntax In [154]: [ (x, y) for x in range(5) for y in range(5, 10) ] # 表达式只能有一个 Out[154]: [(0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9)] In [155]: In [161]: [ (x, y) for x in range(5) if x > 2 for y in range(5, 10) ] Out[161]: [(3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9)] In [159]: [ (x, y) for x in range(5) for y in range(5, 10) if x > 2 ] Out[159]: [(3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9)]
解析式也可以嵌套:
In [163]: [ x ** 2 for x in [ x for x in [1, 2, 3] ] ] Out[163]: [1, 4, 9]
一眼看不出解析式的结果是什么时候,就不要用解析式
2、三元表达式
expr1 if 条件子句 else expr2
当条件蛮族时,返回expr1 或则返回expr2
In [165]: x = 3 In [166]: x ** 2 if x % 2 == 0 else x ** 3 Out[166]: 27 In [172]: name = "xxj" if 3 > 0 else "xj" In [173]: name Out[173]: ‘xxj‘ In [174]: name = "xxj" if 3 <0 else "xj" In [175]: name Out[175]: ‘xj‘ In [176]: name = "xxj" if 3 > 0 else name = "xj" File "<ipython-input-176-36d6075bedd6>", line 1 name = "xxj" if 3 > 0 else name = "xj" ^ SyntaxError: can‘t assign to conditional expression
列表解析和三元表达式结合:
In [180]: [ x ** 2 if x % 2 == 0 else x ** 3 for x in [1, 2, 3, 4 ] ] Out[180]: [1, 4, 27, 16] In [178]: [ x ** 2 for x in [ 1, 2, 3, 4 ] if x % 2 == 0 else x ** 3 ] File "<ipython-input-178-63a49a9bdf59>", line 1 [ x ** 2 for x in [ 1, 2, 3, 4 ] if x % 2 == 0 else x ** 3 ] ^ SyntaxError: invalid syntax
3、生成器解析
列表解析返回的是list
In [8]: ( x ** 2 for x in range(1000) ) Out[8]: <generator object <genexpr> at 0x7f770e8eeb48> In [9]: g = ( x ** 2 for x in range(1000) ) In [10]: type(g) Out[10]: generator In [11]: help(g) In [12]: g.__next__ Out[12]: <method-wrapper ‘__next__‘ of generator object at 0x7f770de47b48> In [13]: g.__next__() Out[13]: 0 In [14]: g.__next__() Out[14]: 1 In [15]: g.__next__() Out[15]: 4 In [16]: next(g) Out[16]: 9 In [17]: next(g) Out[17]: 16
生成器解析返回一个生成器
生成器在取值的时候才计算
生成器解析式和列表解析式除了中括号改成小括号其它的完全一样
4、集合解析式
集合解析返回一个集合
集合解析式和列表解析式除了中括号改成小括号其它的完全一样
In [24]: [ x ** 2 for x in range(5) ] Out[24]: [0, 1, 4, 9, 16] In [25]: ( x ** 2 for x in range(5) ) Out[25]: <generator object <genexpr> at 0x7f770dd5c7d8> In [27]: { x ** 2 for x in range(5) } Out[27]: {0, 1, 4, 9, 16}
5、字典解析式
字典解析返回一个集合
In [28]: { str(x):x ** 2 for x in range(5) } Out[28]: {‘0‘: 0, ‘1‘: 1, ‘2‘: 4, ‘3‘: 9, ‘4‘: 16} In [32]: { str(x + 1):x ** 2 for x in range(5) } Out[32]: {‘1‘: 0, ‘2‘: 1, ‘3‘: 4, ‘4‘: 9, ‘5‘: 16} In [46]: { k:v for k, v in [(‘a‘, 1), (‘b‘, 2)] } Out[46]: {‘a‘: 1, ‘b‘: 2}
以上是关于python13解析式的主要内容,如果未能解决你的问题,请参考以下文章
Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段