¸üÐÂʱ¼ä:2017Äê12ÔÂ26ÈÕ15ʱ25·Ö À´Ô´:ÀÖÓã²¥¿Í ä¯ÀÀ´ÎÊý:
PythonÊÇÒ»ÃÅÓµÓгöÉ«ÓÅÐãµÄ±à³ÌÌØÕ÷µÄº¯Êýʽ±à³ÌÓïÑÔ£¬±à³Ì¸ßÊÖÃÇÀûÓÃÕâЩ¼¼ÇÉÌá¸ß±à³ÌËÙ¶È£¬¸Ä½øÈí¼þÖÊÁ¿¡£¶ø±à³Ì³õѧÕßÒ²ÄÜ´ÓÕâЩ¼ò½àµÄÓï·¨ÖÐѧµ½ÕâÃűà³ÌÓïÑÔµÄÕæÚС£ÏÂÃæ¸ø´ó¼Ò½éÉÜ10¸öPythonµÄµ¥Ðк¯Êýʽ´úÂ룬ϣÍû´ó¼ÒÓÐËùÊÕ»ñ£º
1¡¢ÈÃÁбíÖеÄÿ¸öÔªËØ¶¼³ËÒÔ2
print map(lambda x: x * 2, range(1,11))
2¡¢ÇóÁбíÖеÄËùÓÐÔªËØÖ®ºÍ
print sum(range(1,1001))
3¡¢ÅжÏÒ»¸ö×Ö·û´®ÖÐÊÇ·ñ´æÔÚijЩ´Ê
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)
4¡¢¶ÁÈ¡Îļþ
print open("ten_one_liners.py").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],([],[]))
7. »ñÈ¡XML web serviceÊý¾Ý²¢·ÖÎö
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")
8. ÕÒµ½ÁбíÖÐ×îС»ò×î´óµÄÒ»¸öÊý×Ö
print min([14, 35, -7, 46, 98])
print max([14, 35, -7, 46, 98])
9. ²¢Ðд¦Àí
import multiprocessing
import math
print list(multiprocessing.Pool(processes=4).map(math.exp,range(1,11)))
10. “Sieve of Eratosthenes”Ëã·¨
°£ÀÍÐË¹ÌØÄáɸ·¨£¬ÊÇÒ»ÖÖÓÉÏ£À°Êýѧ¼Ò°£ÀÍÐË¹ÌØÄáËùÌá³öµÄÒ»ÖÖ¼òµ¥¼ì¶¨ËØÊýµÄËã·¨¡£ÒªµÃµ½×ÔÈ»ÊýnÒÔÄÚµÄÈ«²¿ËØÊý£¬±ØÐë°Ñ²»´óÓÚ¸ùºÅnµÄËùÓÐËØÊýµÄ±¶ÊýÌÞ³ý£¬Ê£ÏµľÍÊÇËØÊý¡£
PythonÀïûÓÐSieve of Eratosthenes²Ù×÷·û£¬µ«Õâ¶ÔÓÚPythonÀ´Ëµ²¢²»ÊÇÄÑÊ¡£
n = 50 #±íʾͳ¼Æ³ö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))))
±±¾©Ð£Çø