String Concatenation
- use join instead of + to concat a list of strings;
#bad
s = ""
for substring in list:
s += substring
#good
"".join(list)
#bad with direct concatenation
out = "<html>" + head + prologue + query + tail + "</html>"
#good with format output
out = "<html>%s%s%s%s</html>" % (head, prologue, query, tail)
Loops
- use map cuz it is written in C, period.
#bad
newlist = []
for word in oldlist:
newlist.append(word.upper())
#good
newlist = map(str.upper, oldlist)
- List comprehension and Generator expressions are very compact and super efficient way to write for loop:
#good: list comprehension
newlist = [s.upper() for s in oldlist]
#good: generator expression
iterator = (s.upper() for s in oldlist)
Avoiding dots
- Both newlist.append and word.upper are function references that are reevaluated each time through the loop.
- we can safely store these function in the first place, and just call them through the loop;
#bad:
newlist = []
for word in oldlist:
newlist.append(word.upper())
#good: store the refered functions
upper = str.upper #store the str.upper function
newlist = []
append = newlist.append #store the newlist.append function
for word in oldlist:
append(upper(word))
Local Variable(Function rocks)
- Python accesses local variables much more efficiently than global variables
- use local variables whenever possible;
- how: creating functions to do a specific operation;
def func():
upper = str.upper
newlist = []
append = newlist.append
for word in oldlist:
append(upper(word))
return newlist
Time consumption of abovementioned methods:
Version Time (seconds)
Basic loop 3.47
Eliminate dots 2.45
Local variable & no dots 1.79
Using map function 0.54
Dict
- dict's optimization is simple but plays an super vital role in python code optimization.
- Suppose you are building a dictionary of word frequencies and you've already broken your text up into a list of words. You might execute something like:
#ordinary
wdict = {}
for word in words:
if word not in wdict:
wdict[word] = 0
wdict[word] += 1
#good
wdict = {}
get = wdict.get
for word in words:
wdict[word] = get(word, 0) + 1
#多使用大名鼎鼎的dict.get(key, defaultValue)方法