学习python 基础都会了 为什么还是做不出项目呢?

python是一门足够简单但又非常强大的程序语言,应用领域甲冠天下,在web开发、web爬虫、科学计算、人工智能、云计算、数据分析、数据挖掘、系统运维、金融、游戏等领域都有完备且成熟的开源方案。一个有编程经验的程序员,从0写一个爬虫系统,通常就是一个下午的时间。恰逢人工智能元年,python无可争议地成为当下最热门的语言之一,越来越多的人涌入到python学习热潮中来。
在这里我还是要介绍一下小编的学习交流的群,有什么不懂的问题,都可以在群里踊跃发言,需要啥资料随时在群文件里面获取自己想要的资料。这个python群就是:623715703 小编期待大家一起进群交流讨论,讲实话还是一个非常适合学习的地方的。各种入门资料啊,进阶资料啊,框架资料啊 爬虫等等,都是有的,风里雨里,小编都在群里等你
然而,基础语法都知道,代码也基本能读懂,但是动手能力差,写代码没有任何头绪,开源项目中的代码稍微复杂就难以理解,这是很多初学者面临的困惑。国内 python 教程良莠不齐,大多数仅停留在基础语法层面,这导致很多初学者甚至一些富有经验的开发者写出来的代码带有浓重的“口音”(从其它语言转移过来的编程思维)
迭代器
怎样齐头并进并行的遍历多个集合
in [147]: names = [x for x in 'abcdefg'] in [148]: ages = [x for x in range(21, 28)] in [149]: scores = [randint(90,100) for x in range(7)] in [150]: namesout[150]: ['a', 'b', 'c', 'd', 'e', 'f', 'g'] in [151]: agesout[151]: [21, 22, 23, 24, 25, 26, 27] in [152]: scoresout[152]: [93, 90, 95, 97, 91, 93, 92] in [153]: in [153]: zip?docstring:zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]return a list of tuples, where each tuple contains the i-th elementfrom each of the argument sequences. the returned list is truncatedin length to the length of the shortest argument sequence.type: builtin_function_or_method in [154]: for name,age,score in zip(names, ages, scores): ...: print name,age,score ...:a 21 93b 22 90c 23 95d 24 97e 25 91f 26 93g 27 92
怎样串行的遍历多个集合
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'\2-\1-\3', s)out[175]: 'things happend in 08-2017-09, it is a sunddy'
列表join时如果有数字元素怎么办
in [176]: print '\t'.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,都非常方便。 总之,一点要不断的编程,不断写思考,就能提升自己的功力.加油