python_列表生成式

2018-01-14  本文已影响0人  射手再见蓝天575

列表生成式的好处

列表生成式的格式

[<元素的输出格式> <for...in循环>]
循环的省略
例子:

>>> [s for s in ('momo','huohuo',89,'54re') if isinstance(s,str)]
['momo', 'huohuo', '54re']
>>> [s+y for s in "ABC" for y in 'tmd']
['At', 'Am', 'Ad', 'Bt', 'Bm', 'Bd', 'Ct', 'Cm', 'Cd']
上一篇 下一篇

猜你喜欢

热点阅读