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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 8,543评论 0 23
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,296评论 5 6
  • 突然很想把所有有关记忆的记录全都删除掉,才不会再让自己如此反复煎熬,是空白才没有多余。每每笔尖的丰盛期是心灵...
    转站地铁阅读 184评论 0 1
  • 文/圆小圈儿 N年前的往事,提笔前曾多次想修改,然而,还是忠于初心,尽管不完美,毕竟那是青春的足迹! 思念一个人 ...
    圆小圈儿阅读 116评论 0 4
  • 品味《朗读者》 我们的生活层层叠叠,下一层紧挨着另一层,以至于我们老是在新鲜的遭际中碰触到过去的旧痕,而过去既非完...
    毛各里阅读 426评论 0 2