some coding for python which one line to finish a common
- Product 2 for every element in list
1
| print map(lambda x:x*2, range(1,11))
|
- Sum of all elements in list
1
| print sum(range(1,1001))
|
- Check if there are some world in a string
1 2 3 4
| 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)
|
1
| print open('ten_one_lines.py').readlines()
|
1
| print map(lambda x: "Happy Birthday to " + ("you" if x!=2 else "dear Name"), range(4))
|
1
| print reduce(lambda (a,b),c: (a+[c],b) if c > 60 else (a,b+[c]), [49, 58, 76, 82, 88. 90], ([], []))
|
- Obtin XML web service data and analysis
1 2 3 4
| from xml.dom.minidom import parse, parseString import urllib2
print parse(urllib2.urlopen('http://search.twitter.com/..')).toprettyxml(encoding="utf-8")
|
- Find the max and min data in list
1 2
| print min([14,35,-1,46,38]) print max([14,35,-1,46,38])
|
1 2 3 4
| import multiprocessing import math
print list(mulriprocessing.Pool(pricesses=4).map(math.exp, range(1, 11)))
|
- “Sieve of Eratosthenes” algorithm
1 2
| n = 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))))
|