美文网首页
python_列表生成式

python_列表生成式

作者: 射手再见蓝天575 | 来源:发表于2018-01-14 22:41 被阅读0次

列表生成式的好处

  • 代码量少
  • 效率高

列表生成式的格式

[<元素的输出格式> <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']

相关文章

网友评论

      本文标题:python_列表生成式

      本文链接:https://www.haomeiwen.com/subject/pmbloxtx.html