• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
火磷
Memory will fade,but not notes.
博客园    首页    新随笔    联系   管理    订阅  订阅
python中列表生成式

1.简介

列表生成式即List Comprehensions,是Python中用于创建list的生成式。

2.示例

[表达式  循环体  条件语句]

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 print [x * x for x in range(1, 11)]
 5 print [x * x for x in range(1, 11) if x % 2 == 0]
 6 print [m + n for m in 'ABC' for n in 'XYZ']
 7 
 8 d = {'x': 'A', 'y': 'B', 'z': 'C'}
 9 print [k + '=' + v for k, v in d.items()]
10 
11 L = ['Hello', 'World', 'IBM', 'Apple']
12 print [s.lower() for s in L]

结果如下:

 

posted on 2017-08-17 10:32  火磷  阅读(309)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3