没学过python、但是还是有公司要-学习python 基础都会了 为什么还是做不出项目呢?...

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

Python是一门足够简单但又非常强大的程序语言,应用领域甲冠天下,在WEB开发、WEB爬虫、科学计算、人工智能、云计算、数据分析、数据挖掘、系统运维、金融、游戏等领域都有完备且成熟的开源方案。一个有编程经验的程序员,从0写一个爬虫系统,通常就是一个下午的时间。恰逢人工智能元年,Python无可争议地成为当下最热门的语言之一,越来越多的人涌入到Python学习热潮中来。

然而,基础语法都知道,代码也基本能读懂,但是动手能力差,写代码没有任何头绪,开源项目中的代码稍微复杂就难以理解,这是很多初学者面临的困惑。国内 Python 教程良莠不齐,大多数仅停留在基础语法层面,这导致很多初学者甚至一些富有经验的开发者写出来的代码带有浓重的"口音”(从其它语言转移过来的编程思维)

a540d5853b9240c594c246a64c482495.jpeg

迭代器

怎样齐头并进并行的遍历多个集合

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'--', 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'

577b82acc99443f1873ff9b577341153.jpeg

并发编程

如何使用多线程

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

4b294eba82b743579a917a51fce8f01c.jpeg

学Python 说千遍道万遍,不如自己实战一遍,比如做一个爬虫的项目,一方面可以掌握网页爬虫的技巧,同时可以提高自己代码的功力,当你的代码量几千行的时候,一点会需要重构,一定需要加一些设计模式,也可以自己找一些数据分析的项目,加深自己对pandas的理解。 也可以自己搭建一个web理由Django,Flask,都非常方便。 总之,一点要不断的编程,不断写思考,就能提升自己的功力.加油返回搜狐,查看更多

责任编辑:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值