标签:
转:http://www.vaikan.com/10-python-one-liners-to-impress-your-friends/
几年前,函数式编程的复兴正值巅峰,一篇介绍 Scala 中 10 个单行函数式代码的博文在网上走红。很快地,一系列使用其他语言实现这些单行代码的文章也随之出现,比如 Haskell, Ruby, Groovy, Clojure, Python, C#, F#, CoffeeScript。
每篇文章都令人印象深刻的揭示了这些语言中一些出色优秀的编程特征。编程高手们利用这些技巧提高编程速度、改进软件质量,编程初学者能从这些简洁的预防中学到各种编程语言的真谛。本《震惊小伙伴的单行代码系列》将逐一介绍这些各种编程语言单行代码文章,供大家学习参考。
print map(lambda x: x * 2, range(1,11))
print sum(range(1,1001))
wordlist = ["scala", "akka", "play framework", "sbt", "typesafe"] tweet = "This is an example tweet talking about scala and sbt." print map(lambda x: x in tweet.split(),wordlist)
print open("ten_one_liners.py").readlines()
print map(lambda x: "Happy Birthday to " + ("you" if x != 2 else "dear Name"),range(4))
print reduce(lambda(a,b),c: (a+[c],b) if c > 60 else (a,b + [c]), [49, 58, 76, 82, 88, 90],([],[]))
from xml.dom.minidom import parse, parseString import urllib2 # 注意,我将它转换成XML格式化并打印出来 print parse(urllib2.urlopen("http://search.twitter.com/search.atom?&q=python")).toprettyxml(encoding="utf-8")
print min([14, 35, -7, 46, 98]) print max([14, 35, -7, 46, 98])
import multiprocessing import math print list(multiprocessing.Pool(processes=4).map(math.exp,range(1,11)))
Python里没有Sieve of Eratosthenes操作符,但这对于Python来说并不是难事。
n = 50 # We want to find prime numbers between 2 and 50 print sorted(set(range(2,n+1)).difference(set((p * f) for p in range(2,int(n**0.5) + 2) for f in range(2,(n/p)+1))))
标签:
原文地址:http://www.cnblogs.com/doinone/p/5486565.html