Ext.: http://www.vaikan.com/10-python-one-liners-to-impress-your-friends/
A few years ago, the revival of functional programming peaked, and a blog post introducing 10 single-line functional code in Scala became popular online. Soon, a series of articles that implement these single-line codes in other languages also appear, such as Haskell, Ruby, Groovy, Clojure, Python, C #, F #, Coffeescript.
Each article is an impressive revelation of some of the best programming features in these languages. Programmers use these techniques to improve programming speed and improve software quality, which beginners can learn from these simple precautions to the true meaning of various programming languages. This "shocked small partner single-line code series" will introduce each of these various programming language single-line code article, for everyone to learn the reference.
1. Multiply each element in the list by 2
Print map (Lambda x:x * 2, Range (1,11))
2, the sum of all the elements in the list
Print sum (range (1,1001))
3. Determine if there are certain words in a string
Wordlist = ["Scala","Akka","Play Framework","SBT","Typesafe"]tweet="This is a example tweet talking about Scala and SBT."PrintMapLambdaX:xinchTweet.split (), wordlist)
4. Read the file
Print open ("ten_one_liners.py"). ReadLines ()
5, wish you a happy birthday!
Print map (Lambda"" + ("you"if Else " Dear Name "), Range (4))
6. Filter the values in the list
print reduce (lambdaifelse (A, B + [C]), [49, 58, 76, 82, 88, 90], ([],[]))
7. Get the XML Web service data and analyze
from Import Parse, parsestring Import Urllib2 # Note that I convert it to XML format and print it out Print Parse (urllib2.urlopen ("Http://search.twitter.com/search.atom?&q=python ")). Toprettyxml (encoding="utf-8")
8. Find the smallest or largest number in the list
print min ([7,, 98])print max ([14, 35,-7, 46, 98])
9. Parallel processing
Import Multiprocessing Import Math Print list (multiprocessing. Pool (processes=4). Map (Math.exp,range (1,11)))
"Sieve of Eratosthenes" algorithm
Python does not have the sieve of Eratosthenes operator, but it is not difficult for python.
# We want to find prime numbers between 2 and Print for inch for in range (2, (n/p) +1))))
Turn: Shocked small partner's single-line code python chapter