Python Snippet

Useful Python Code Snippet

Control and Loop:

# python if
lval = left[l] if l < len(left) else None
expr if test else expr
# python enumeration
for i, val in enumerate(['a','b','c']):
    print i, val
map = dict() # assume we have something in map
for key in map:
    print key

for value in map.itervalues():
    print value

for k,v in map.iteritems():
    print k,v

for x, y in [(1, 1), (2, 4), (3, 9)]:
    print x, y

list comprehension

# eg1
[int(v) for v in str1.split(".")]

# eg2
v1, v2 = (map(int, v.split('.')) for v in (version1, version2))

# eg3:
[x * x for x in range(1, 11)]

# eg4:
[x * x for x in range(1, 11) if x % 2 == 0]

# eg5:
>>> [m + n for m in 'ABC' for n in 'XYZ']
['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']
    # note: the result's order reveal how this nested for works

# eg6:
>>> import os
>>> [d for d in os.listdir('.')] # os.listdir可以列出文件和目录
['.emacs.d', '.ssh', '.Trash', 'Adlm', 'Applications', 'Desktop', 'Documents', 'Downloads', 'Library', 'Movies', 'Music', 'Pictures', 'Public', 'VirtualBox VMs', 'Workspace', 'XCode']


Python string

  • 'string{}'.format(variable)

'{} checking if function exists: {}'.format(emoji, function_name)

#!/usr/bin/python
sub1 = "python string!"
sub2 = "an arg"

a = "i am a %s" % sub1
b = "i am a {0}".format(sub1)

c = "with %(kwarg)s!" % {'kwarg':sub2}
d = "with {kwarg}!".format(kwarg=sub2)

print a    # "i am a python string!"
print b    # "i am a python string!"
print c    # "with an arg!"
print d    # "with an arg!"

List, Set, Dictionary, Tuple

dictionary

Initialize and Update dictionary value at single line

dic[num] = dic.get(num, 0)+1

Set

List


Generator

generator和函数的执行流程不一样。函数是顺序执行,遇到return语句或者最后一行函数语句就返回。而变成generator的函数,在每次调用next()
的时候执行,遇到yield
语句返回,再次执行时从上次返回的yield
语句处继续执行。

g = (x * x for x in range(10))
# can call g.next() to keep printing elements
for n in g:
    print n
"""
def F(n):
    if n == 0: return 0
    elif n == 1: return 1
    else: return F(n-1)+F(n-2)
"""
def fib(max):
    n, a, b = 0, 0, 1
    while n < max:
        yield b # only yield b, but update a,b together!!
        a, b = b, a + b  # this is interesting, although in Fib seq, the first two
          # elements are 1,1, but here we initialize a,b=0,1; because this will
          # make our code nicer
          # the logic here is to:
          #   a = b
          #   b = a + b
        n = n + 1

higher order function

map, reduce, filter, sorted etc ...

def is_odd(n):
    return n % 2 == 1
filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15])

def not_empty(s):
    return s and s.strip()
filter(not_empty, ['A', '', 'B', None, 'C', ' '])  # 结果: ['A', 'B', 'C']
>>> sorted([36, 5, 12, 9, 21])
[5, 9, 12, 21, 36]
def reversed_cmp(x, y):
    if x > y:
        return -1
    if x < y:
        return 1
    return 0
>>> sorted([36, 5, 12, 9, 21], reversed_cmp)
[36, 21, 12, 9, 5]
>>> map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9])
[1, 4, 9, 16, 25, 36, 49, 64, 81]

Conversion between String and List

** List to String **

mylist = ['spam', 'ham', 'eggs']

','.join(mylist)

'\n'.join(mylist)

''.join(mylist)

** String to List **


zip and unzip

>>> a = [1, 2, 3, 4, 5]
>>> b = [2, 2, 9, 0]
>>> zip(a,b)
[(1, 2), (2, 2), (3, 9), (4, 0)]

>>> zip(*zip(a,b)) # * used to unpack argument (same as * in argument parsing)
[(1, 2, 3, 4), (2, 2, 9, 0)]

>>> strs = ['abc','def','sdd']
>>> zip(*strs)
[('a', 'd', 's'), ('b', 'e', 'd'), ('c', 'f', 'd')]  # each element is a tuple
>>> zip(strs)
[('abc',), ('def',), ('sdd',)]

>>> strs = ['abc','def','sdd','k']
>>> zip(*strs)
[('a', 'd', 's', 'k')]

Standard Input and Output

input() uses raw_input to read a string of data, and then attempts to evaluate it as if it were a Python program, and then returns the value that results.

# this is how we read from standard input in Python (hackerrank).
x = raw_input('What is your name?')
x = input('What are the first 10 perfect squares? ')

Python largest and smallest number

smallest = float('inf')
largest = float('-inf')

Advanced data structure:

defaultdict

  • A defaultdict works exactly like a normal dict, but it is initialized with a function (“default factory”) that takes no arguments and provides the default value for a nonexistent key.
  • A defaultdict will never raise a KeyError. Any key that does not exist gets the value returned by the default factory.
  • Be sure to pass the function object to defaultdict(). Do not call the function, i.e. defaultdict(func), not defaultdict(func()).
  • In the following example, a defaultdict is used for counting. The default factory is int, which in turn has a default value of zero. (Note: “lambda: 0″ would also work in this situation).
>>> from collections import defaultdict
>>> ice_cream = defaultdict(lambda: 'Vanilla')
>>>
>>> ice_cream = defaultdict(lambda: 'Vanilla')
>>> ice_cream['Sarah'] = 'Chunky Monkey'
>>> ice_cream['Abdul'] = 'Butter Pecan'
>>> print ice_cream['Sarah']
Chunky Monkey
>>> print ice_cream['Joe']
Vanilla

>>> food_count = defaultdict(int) # default value of int is 0

counter


Others

def compareVersion(self, version1, version2):
    v1, v2 = (map(int, v.split('.')) for v in (version1, version2))
        # (list_comprehension) return a generator
        # then v1, v2 take the result out of generator (will be list)
    d = len(v2) - len(v1)
    return cmp(v1 + [0]*d, v2 + [0]*-d)
      # [0]*-3 is still [0]
      # purpose of this list multiplication is align the length of two list
      # cmp can work on list, compare element by element
      # I feel this step is not needed, as cmp anyway do comparison element by element

I/O

Read text

Read csv

res = pandas.read_csv('res-large-tmp.csv', sep=',', header=None)

Read json

with open("moviedata.json") as json_file:
    movies = json.load(json_file, parse_float = decimal.Decimal)
    # if string, use json.loads()
    for movie in movies:
        year = int(movie['year'])
        title = movie['title']
        info = movie['info']
import json
print json.dumps(json_format_string) # print out json format
import os

fileList = ["dir/dir2/dir3/dir3/file.txt",
    "dir/dir2/dir3/file.txt",
    "example/directory/path/file.txt"]

for file in fileList:
    dir = os.path.dirname(file)
    # create directory if it does not exist
    if not os.path.exists(dir):
        os.makedirs(dir)
    # Create blank file if it does not exist
    with open(file, "w"):
        pass

Python commandline parser

https://docs.python.org/2/howto/argparse.html

  • positional argument (required)
  • optional argument
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo", help="echo the string you use here" )
parser.add_argument("-v", "--verbosity", help="increase output verbosity")
args = parser.parse_args()

print args.echo
if args.verbosity: 
  print "verbosity turned on"

os.path and python path management

The following code can get get file's sitting directory, instead of the directory where we run python

# doesn't matter where we run our code:
    currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    parentdir = os.path.dirname(currentdir)
    parpardir = os.path.dirname(parentdir)
    sys.path.insert(0,parentdir) 
    print(os.path.abspath(inspect.getfile(inspect.currentframe()))) # /Users/swu233/Work/i2_lookup_table/util/try_path.py
    print(currentdir)  # /Users/swu233/Work/i2_lookup_table/util
    print(parentdir) # /Users/swu233/Work/i2_lookup_table
    print(parpardir) # /Users/swu233/Work

# change wrt to where we running our script
    print(os.path.abspath("."))  # change wrt running dir

# some other os.path function:
    print(os.path.basename(currentdir))  # util
    print(os.path.isfile(currentdir))  # False
    print(os.path.isfile(os.path.abspath(inspect.getfile(inspect.currentframe())))) # True

你可能感兴趣的:(Python Snippet)