关于ord()函数的使用、列表解析、filter()

实际运行结果如下:
[]
>>> for x in 'span':
res.append(ord('x'))
>>> res
[120, 120, 120, 120]
>>> for x in 'span':
res.append(ord(x))
>>> res
[120, 120, 120, 120, 115, 112, 97, 110]
>>>

这里应用对象的是序列,所以必须加[ ]
>>> [x **2 for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> x **2 for x in range(10) SyntaxError: invalid syntax >>>
filter的用法:
>>> list(lambda x:x%2 == 0)
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
list(lambda x:x%2 == 0)
TypeError: 'function' object is not iterable
>>> list(filter(lambda x: x%2 == 0,range(5)))
[0, 2, 4]
>>> list(lambda x:x%2 == 0)
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
list(lambda x:x%2 == 0)
TypeError: 'function' object is not iterable
>>> list(filter(lambda x: x%2 == 0,range(5)))
[0, 2, 4]
1 >>> list(filter(lambda x: x%2 == 0,range(5))) 2 [0, 2, 4] 3 >>> list(filter(lambda:b b%2 = 0,range(101))) 4 SyntaxError: invalid syntax 5 >>> list(filter(lambda b :b%2 = 0,range(101))) 6 7 SyntaxError: unexpected indent 8 >>> list(filter(lambda b :b%2 = 0,range(101))) 9 SyntaxError: invalid character in identifier 10 >>> range(101) 11 range(0, 101) 12 >>> list(filter(lambda b :b%2 = 0, range(101))) 13 SyntaxError: expression cannot contain assignment, perhaps you meant "=="? 14 >>> list(lambda b:b %2 == 0,range(101)) 15 Traceback (most recent call last): 16 File "<pyshell#71>", line 1, in <module> 17 list(lambda b:b %2 == 0,range(101)) 18 TypeError: list expected at most 1 argument, got 2 19 >>> list(filter((lambda b : b% 2 ==0 ),range(101))) 20 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100] 21 >>>
心有猛虎,当细嗅蔷薇
浙公网安备 33010602011771号