all the following have the same behavior
1.
>>> res = []
>>> for x in 'spam':
res.append(ord(x)) # Manual results collection
2.
>>> res = list(map(ord, 'spam')) # Apply function to sequence (or other)
3
>>> res = [ord(x) for x in 'spam'] # Apply expression to sequence (or other)
Formal comprehension syntax:
[expresion for target in iterable]
more generally:
[ expression for target1 in iterable1 if condition1
for target2 in iterable2 if condition2 ...
for targetN in iterableN if conditionN ]


雷达卡




thanks!
京公网安备 11010802022788号







