有趣的超短Python代码
2016-07-12 23:06
281 查看
-- coding:utf-8 --
python2.7
1让列表的每个元素都乘以2
print map(lambda x: x*2,range(1,11))2求列表中所有元素之和
print sum(range(1,100)) #range(1,100)代表从1到100(不包含100)3,判断一个字符串中是否存在某些词
wordlist = [“scala”,”akka”,”play framework”,”sbt”,”typesafe”]tweet = “This is an example tweet talking about scala an sbt”
print map(lambda x:x in tweet.split(),wordlist)
Python split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串 这里默认为空格
4,读取文件
print open(“test.txt”).readlines()5,祝你生日快乐
print map(lambda x: “happy birthday to” +(“YOU” if x!=2 else “dear Name”),range(4))6,过滤列表中的数值
print reduce(lambda(a,b),c:(a+[c],b)if c >60 else (a,b+[c]),[49,58,76,82,88,90],([],[]))结果:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
4950
[True, False, False, True, False]
[‘this is a test\n’, ‘no more words’]
[‘happy birthday toYOU’, ‘happy birthday toYOU’, ‘happy birthday todear Name’, ‘happy birthday toYOU’]
([76, 82, 88, 90], [49, 58])
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法