Using Python's built-in defaultdict we can easily define a tree data structure:
def tree(): return defaultdict(tree)
That's it!
from itertools import count, imap | |
import time | |
import sys | |
def stream(lo, hi, f): | |
def approx((a,b,c), n): return (a * n + b) // c | |
def mul((a,b,c), (d,e,f)): return a * d, a * e + b * f, c * f | |
xs = ((n, a*d, d) for n, d, a in imap(f, count(1))) | |
z = 1, 0, 1 | |
while True: |
from multiprocessing import Pool, cpu_count | |
from time import sleep | |
def f(x): | |
for i in range(10): | |
print 'counting %d --- Process:%d ' % (i, x) | |
sleep(1.0+float(x % cpu_count())/5) | |
def main(): |
>>> class AAA(object): | |
... def go(self): | |
... self.one = 'hello' | |
... | |
>>> class BBB(object): | |
... def go(self): | |
... one = 'hello' | |
... | |
>>> a1 = AAA() | |
>>> a1.go() |
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# unzip-gbk.py | |
import os | |
import sys | |
import zipfile | |
print "Processing File " + sys.argv[1] |
# def next() for Python pre-2.6 | |
class Throw(object): pass | |
throw = Throw() # easy sentinel hack | |
def next(iterator, default=throw): | |
"""next(iterator[, default]) | |
Return the next item from the iterator. If default is given | |
and the iterator is exhausted, it is returned instead of | |
raising StopIteration. |
from itertools import groupby | |
# A sample input of a word count problem | |
source = ["Here is the first line in this source", | |
"And Here is the second line in this source", | |
"Welcome to the third line in this source"] | |
# map stage | |
map_result = map(lambda line: [(word.lower(), 1) for word in line.split()], source) | |
# [[('here', 1), ('is', 1), ('the', 1), ('first', 1), | |
# ('line', 1), ('in', 1), ('this', 1), ('source', 1)], | |
# [('and', 1), ('here', 1), ('is', 1), ('the', 1), ('second', 1), |
d = { | |
"A0801_000000_201301": "1,321.8", | |
"A0801_000000_201302": "1,199.8", | |
"A0801_000000_201309": "1,433.4", | |
"A0802_000000_201305": "6,688.3", | |
"A0802_000000_201306": "8,085.2", | |
"A0802_000000_201307": "9,481.0", | |
"A0802_000000_201308": "10,878.4", | |
"A0802_000000_201309": "12,311.8", | |
"A0802_000000_201310": "13,739.9", |
Using Python's built-in defaultdict we can easily define a tree data structure:
def tree(): return defaultdict(tree)
That's it!
from functools import partial | |
def func(a, b, c): | |
print a, b, c | |
func_fix_b = partial(func, b="what you want,") | |
func_fix_b(a="Here is", c="take it") | |
# Here is what you want, take it | |
func_fix_b("Here is", c="take it") | |
# Here is what you want, take it |