码迷,mamicode.com
首页 > 编程语言 > 详细

python学习---列表生成式

时间:2017-10-10 13:12:23      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:div   env   color   col   coding   log   学习   class   nbsp   

 1 #!/usr/bin/env python3
 2 # -*- coding: utf-8 -*-
 3 
 4 #列表生成式即List Comprehensions,是Python内置的非常简单却
 5 #强大的可以用来创建list的生成式
 6 print([x * x for x in range(1,21,2)])
 7 print([x * x for x in range(1,11) if x % 2 == 0])
 8 print([m + n for m in ‘ABC‘ for n in ‘XYZ‘])
 9 
10 d = {‘x‘: ‘A‘, ‘y‘: ‘B‘, ‘z‘: ‘C‘}
11 print([k + ‘=‘ + v for k,v in d.items()])
12 
13 L = [‘Hello‘, ‘World‘, ‘IBM‘, ‘Apple‘]
14 print([s.lower() for s in L])
15 
16 L2 = [‘Hello‘, ‘World‘, 18, ‘IBM‘, ‘Apple‘]
17 def lower2(s):
18     if isinstance(s,str):
19         return s
20     else:
21         return s
22 print([lower2(s) for s in L2])

 

python学习---列表生成式

标签:div   env   color   col   coding   log   学习   class   nbsp   

原文地址:http://www.cnblogs.com/hayden1106/p/7644866.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!