Python

Data

data type

Boolean, Int, Long, Float

Data Structure

Basic DS:

Import collections:

  • deque, Counter, OrderedDict, defaultdict, namedtuple

  • reference

Higher level:

  • heapq, Queue

Need Implement:

  • Trie

Article:

[1] basic

[2] basic & import

[3] heapq

Implement

####Heap

####Trie

Coding

Pythonic

#empty string can use not a
a = ""
if not a:
	#run /w empty a
#Created object can have dynamic attributes, built-in DS cannot
class Object(object):
	pass
aa = Object
aa.flag = True #add flag dynamically
if hasattr(aa,'flag'):
	print aa.flag #print True

bb = {}
try:
	bb.flag = True
except Exception, e:
	print e #'dict' object has no attribute 'flag'

Higher Performance

String Related

# use join instead of +=
a = ["as", "you", "sunny"]
res = " ".join(a)

#print
a = {'a': 1, 'b':2}
res = "hello %(a)s and %(b)s" %a

Loop Related

# map > local & dot > basic
# map(func, iter) call func for every iter, not good for overlap operation
# record .func as lcoal variable
# use xrange() instead of range()
oldlist = "asdfs"
newlist = []
newlist = map(str.upper, oldlist)

upper = str.upper
append = newlist.append()
for var in oldlist:
    append(upper(var))

Dictionary

# try is better than if
# use .get(index, 0)
# use collections.defaultdict

Calculate

# + > >> or << > * or /

Profiling

# several module

Memory access

Touch address

# id()

# iterator
a = [1,2,3,4]
b = iter(a)
print next(b)

Tricks

  • x = a or b

    #one of a,b is equal to None
    if a or b:
        x = a or b
  • hasattr() check whether it has additional attributes

    while newHead != None:
        if hasattr(newHead,'flag') and newHead.flag == True:
            return newHead
            newHead.flag = True
        return None
  • print

    • without newline

      #the last , is indicate
      print str(node.value) , "->" ,
    • any format

      a = [1,2,3]
      print "the list:%r" %a

Structure & Design Skill

  • OOP

    • class variable & object variable

    • no virtual function like C++

    • don't touch member directly

  • Structure

    • multi-file use os and sys module to change path

    • module

      • __name__: __main__ or module

Fault List

  • and instead of &&

  • create intersection link list

    cannot append multiple nodes one by one to 2 link lists, because when we append them to the 1st list, all nodes are linked together, for the 2nd list, there will be circle.

  • file name cannot be string.py

Reference

[1] Pythonic

[2] Improve Performance

[3] Except System