怎样串行的遍历多个集合
In [158]: lista = (randint(60,70) for x in range(10)) In [159]: list(lista)Out[159]: [65, 60, 62, 64, 63, 60, 68, 67, 61, 62] In [160]: listb = [randint(90,100) for x in range(20)] In [161]: listbOut[161]:[98, 96, 97, 98, 95, 95, 90, 99, 92, 92, 99, 92, 100, 95, 100, 100, 93, 91, 92, 98] In [163]: from itertools import chain In [164]: chain?Docstring:chain(*iterables) --> chain object Return a chain object whose .next() method returns elements from thefirst iterable until it is exhausted, then elements from the nextiterable, until all of the iterables are exhausted.File: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/itertools.soType: type In [165]: for x in chain(lista, listb): ...: print x, ...:98 96 97 98 95 95 90 99 92 92 99 92 100 95 100 100 93 91 92 98
字符串
使用多种分隔符拆分字符串
In [166]: s = 'a,b;c/d' In [167]: import re In [169]: re.sub?Signature: re.sub(pattern, repl, string, count=0, flags=0)Docstring:Return the string obtained by replacing the leftmostnon-overlapping occurrences of the pattern in string by thereplacement repl. repl can be either a string or a callable;if a string, backslash escapes in it are processed. If it isa callable, it's passed the match object and must returna replacement string to be used.File: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.pyType: function In [171]: re.sub(r'[,;/]', '-', s)Out[171]: 'a-b-c-d'
如果进行字符串的模糊搜索与部分替换
In [172]: s = 'things happend in 2017-08-09, it is a sunddy' In [175]: re.sub(r'(d{4})-(d{2})-(d{2})', r'--', s)Out[175]: 'things happend in 08-2017-09, it is a sunddy'
列表JOIN时如果有数字元素怎么办
In [176]: print ' '.join([str(x) for x in ['a','b',33,4.0,'e']])a b 33 4.0 e
文件
如何使用临时文件
In [186]: from tempfile import TemporaryFile,NamedTemporaryFileIn [187]: t = TemporaryFile()In [188]: t.write('aa')In [189]: t.close()In [191]: NamedTemporaryFile?Signature: NamedTemporaryFile(mode='w+b', bufsize=-1, suffix='', prefix='tmp', dir=None, delete=True)Docstring:Create and return a temporary file.Arguments:'prefix', 'suffix', 'dir' -- as for mkstemp.'mode' -- the mode argument to os.fdopen (default "w+b").'bufsize' -- the buffer size argument to os.fdopen (default -1).'delete' -- whether the file is deleted on close (default True).The file is created as mkstemp() would do it.Returns an object with a file-like interface; the name of the fileis accessible as its 'name' attribute. The file will be automaticallydeleted when it is closed unless the 'delete' argument is set to False.File: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.pyType: functionIn [192]: t = NamedTemporaryFile()In [193]: t.write("a")In [194]: t.nameOut[194]: '/var/folders/sc/rpg0yq054hb7vdr83ms1rp2w0000gn/T/tmpQIONuU'
并发编程
如何使用多线程
In [8]: cat multi_threading.pyfrom threading import Thread def func(x): print x, x*x*xts = []for x in range(10): t = Thread(target=func, args=(x,)) t.start() ts.append(t) for t in ts: t.join() print 'main thread over' In [9]: %run multi_threading.py0 01 12 83 274 645 1256 2167 3438 5129 729main thread over
上一中是直接用函数执行,第二种是先创建一个类继承自Thread类
In [18]: cat multi_threading_class.pyfrom threading import Thread class MyThread(Thread): def __init__(self, x): Thread.__init__(self) self.x = x def run(self): print self.x, self.x*self.x*self.xts = []for x in range(10): t = MyThread(x) t.start() ts.append(t) for t in ts: t.join() print 'main thread over' In [19]: %run multi_threading_class.py0 01 12 83 274 645 1256 2167 3438 5129 729main thread over
线程间通信-生产者消费者模式
In [8]: cat producer_consumer.py#coding:utf8 from threading import Thread,currentThreadfrom Queue import Queuefrom time import sleepfrom random import randint,samplefrom itertools import chain class Producer(Thread): def __init__(self, queue): Thread.__init__(self) self.queue = queue def run(self): for x in range(5): sleep(randint(1,3)) ele = sample('abcdefg',1)[0] print 'producer %s: %s'%(currentThread().name, ele) self.queue.put(ele) class Consumer(Thread): def __init__(self, queue): Thread.__init__(self) self.setDaemon(True) self.queue = queue def run(self): while(True): e = self.queue.get() sleep(1) print 'consumer %s: %s' % (currentThread().name, e)queue = Queue()tps = []for x in range(3): tp = Producer(queue) tp.start() tps.append(tp) for x in range(2): tc = Consumer(queue) tc.start() for t in tps: t.join() print 'main thread over' In [9]: %run producer_consumer.pyproducer Thread-301: a^Cconsumer Thread-304: aproducer Thread-302: cproducer Thread-303: gproducer Thread-301: cconsumer Thread-304: gconsumer Thread-305: cproducer Thread-303: b
学Python 说千遍道万遍,不如自己实战一遍,比如做一个爬虫的项目,一方面可以掌握网页爬虫的技巧,同时可以提高自己代码的功力,当你的代码量几千行的时候,一点会需要重构,一定需要加一些设计模式,也可以自己找一些数据分析的项目,加深自己对pandas的理解。 也可以自己搭建一个web理由Django,Flask,都非常方便。 总之,一点要不断的编程,不断写思考,就能提升自己的功力.加油