Python练习-- List Comprehensions
2016-02-23 19:25
435 查看
#!/usr/bin/python # -*- coding: utf-8 -*- print('List Comprehensions!!') mylist=[x*x for x in range(1,11)] print(mylist) mylist2 = [x*x for x in range(1,10) if x%2 == 0] print(mylist2) mylist3 = [m+n 4000 for m in 'ABC' for n in 'XYZ'] print(mylist3) import os mylist4 =[d for d in os.listdir('.')] print(mylist4) d = {'x': 'A', 'y': 'B', 'z': 'C' } for k,v in d.items(): print(k,'=',v) mylist5 = [k+'='+v for k,v in d.items() ] print(mylist5) L = ['Hello', 'World', 'IBM', 'Apple'] lowerL = [s.lower() for s in L] print(lowerL) L2 = ['Hello', 'World', 18, 'Apple', None] lowerL2 = [s.lower() for s in L2 if isinstance(s,str)] print(lowerL2)
相关文章推荐
- 《笨办法学Python》 第43课手记
- Python导入matplotlib错误提示ImportError: matplotlib requires pyparsing
- Python socket超时
- 运行独立python 引用其他目录包
- python第七天
- Windows环境下新浪SAE本地开发环境搭建及简单测试
- os x + virtualenv + openCV2 + python2.7 环境打造
- 【LEETCODE】148- Sort List [Python]
- python 转化文件编码 utf8
- python之疑惑函数默认初始化
- [Leetcode]@python 95. Unique Binary Search Trees II
- cmd和python的mysql连接方法
- [Leetcode]@python 94. Binary Tree Inorder Traversal
- PyQt4 Python GUI窗体应用程序
- [Leetcode]@python 93. Restore IP Addresses
- python k-近邻算法
- Python练习-- slice
- Python练习-- loop
- Python练习-- list and tuple
- Python练习-- iterator