python学习---列表生成式

 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])

 

posted @ 2017-10-10 11:52  hayden__wang  阅读(113)  评论(0编辑  收藏  举报