神級程序員帶你從零基礎Python入門到進階!(資料非常全)

編程語言 Python 程序員 技術 潭州python 潭州python 2017-09-07

如果你還在入門糾結,如果你還在苦惱怎麼入門python!小編有個建議,可以加小編弄的一個Python交流基地,大家可以進入交流基地:58937142,裡面新手入門資料,可以說從零到項目實戰,都是可以免費獲取的,還有程序員大牛為各位免費解答問題,熱心腸的小夥伴也是蠻多的。不失為是一個交流的的好地方,小編在這裡邀請大家加入我的大家庭。歡迎你的到來。一起交流學習!共同進步!小編等你!

神級程序員帶你從零基礎Python入門到進階!(資料非常全)

python的使用

地址:https://ipython.org/install.html

簡單的安裝方法:pip install ipython

一些方便的使用方法:

  • 輸入要查看的對象,然後輸入一個問號可以查看API,輸入兩個問號可以查看代碼

  • 可以直接調用shell命令,在前面帶上!即可

  • 按Tab可以自動語法補全

  • 最近的命令輸出結果,可以從_、__、___三個變量獲得

  • %hist或者%history查看歷史命令

  • %timeit可以進行命令的執行時間測試

數據結構與算法

列表生成器

In [5]: a=[1,2,3] In [6]: [x*x for x in a if x>1]Out[6]: [4, 9]

集合生成器(和列表生成器相同)

In [7]: a=[1,2,3] In [8]: s = {x*x for x in a if x>1} In [9]: sOut[9]: {4, 9} In [10]: type(s)Out[10]: set

字典生成器

In [11]: a=[1,2,3]In [12]: {str(x):x+1 for x in a if x>1}Out[12]: {'2': 3, '3': 4}

range和xrange的使用

In [13]: range?Docstring:range(stop) -> list of integersrange(start, stop[, step]) -> list of integers In [14]: range(10)Out[14]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [15]: range(3,10)Out[15]: [3, 4, 5, 6, 7, 8, 9] In [16]: xrange?Docstring:xrange(stop) -> xrange objectxrange(start, stop[, step]) -> xrange object In [19]: list(xrange(3,10))Out[19]: [3, 4, 5, 6, 7, 8, 9] In [21]: list(xrange(10))Out[21]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

range和xrange的用法相同,只是range會直接生成一個內存列表,xrange會生成一個生產器,xrange的效率更高,更節省內存

filter用於過濾數據

In [22]: filter?Docstring:filter(function or None, sequence) -> list, tuple, or string Return those items of sequence for which function(item) is true. Iffunction is None, return the items that are true. If sequence is a tupleor string, return the same type, else return a list.Type: builtin_function_or_method In [23]: filter(lambda x:x%3==0, xrange(10))Out[23]: [0, 3, 6, 9]

使用collections.namedtuple給列表或者元組命名

In [24]: from collections import namedtuple In [30]: Point = namedtuple('Point', ['x', 'y']) In [31]: p = Point(11, 22) In [32]: p.__dict__Out[32]: OrderedDict([('x', 11), ('y', 22)]) In [33]: p.xOut[33]: 11 In [34]: p.yOut[34]: 22

random的使用

In [35]: from random import randint In [36]: randint?Signature: randint(a, b)Docstring:Return random integer in range [a, b], including both end points. File: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.pyType: instancemethod In [37]: randint(1,10)Out[37]: 2

統計序列元素的頻度和TOP N

In [42]: from collections import Counter In [43]: Counter? In [44]: c = Counter('aaabbbbccccccddddddeeeeee') In [45]: cOut[45]: Counter({'a': 3, 'b': 4, 'c': 6, 'd': 6, 'e': 6}) In [46]: c.most_common(3)Out[46]: [('c', 6), ('e', 6), ('d', 6)]

將字典按value排序

In [47]: from random import randintIn [48]: keys = 'abcdefg' In [50]: d = {x:randint(90,100) for x in keys}In [51]: dOut[51]: {'a': 90, 'b': 100, 'c': 94, 'd': 97, 'e': 94, 'f': 95, 'g': 91}In [53]: d.items()Out[53]: [('a', 90), ('c', 94), ('b', 100), ('e', 94), ('d', 97), ('g', 91), ('f', 95)]In [54]: sorted?Docstring: sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted listType: builtin_function_or_methodIn [55]: sorted(d.items(), key=lambda x : x[1])Out[55]: [('a', 90), ('g', 91), ('c', 94), ('e', 94), ('f', 95), ('d', 97), ('b', 100)]

獲得多個詞典的key的交集

In [99]: from random import randint, sample In [100]: dd1 = {x:randint(90,100) for x in sample('abcdefghij', 5)} In [101]: dd2 = {x:randint(90,100) for x in sample('abcdefghij', 5)} In [102]: dd3 = {x:randint(90,100) for x in sample('abcdefghij', 5)} In [103]: dd1Out[103]: {'b': 100, 'c': 97, 'd': 100, 'e': 97, 'f': 92} In [104]: dd2Out[104]: {'a': 100, 'c': 90, 'g': 93, 'h': 93, 'j': 90} In [105]: dd3Out[105]: {'c': 96, 'e': 93, 'f': 91, 'h': 97, 'j': 90} In [106]: mp = map(dict.viewkeys, (dd1, dd2, dd3)) In [107]: mpOut[107]:[dict_keys(['c', 'b', 'e', 'd', 'f']),dict_keys(['a', 'h', 'c', 'j', 'g']),dict_keys(['h', 'c', 'j', 'e', 'f'])] In [108]: reduce(lambda x,y: x&y, mp)Out[108]: {'c'}

怎樣讓字典按照插入有序

In [122]: from collections import OrderedDict In [123]: d = OrderedDict() In [124]: d['x'] = 1 In [125]: d['y'] = 2 In [126]: d['a'] = 2 In [127]: d['b'] = 2 In [128]: dOut[128]: OrderedDict([('x', 1), ('y', 2), ('a', 2), ('b', 2)])

怎樣實現長度為N的隊列功能

In [138]: from collections import deque In [139]: deque?Docstring:deque([iterable[, maxlen]]) --> deque object Build an ordered collection with optimized access from its endpoints.File: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.pyType: type In [141]: d = deque([], 3) In [142]: d.append(1) In [143]: d.append(2) In [144]: d.append(3) In [145]: d.append(4) In [146]: dOut[146]: deque([2, 3, 4])

迭代器

怎樣齊頭並進並行的遍歷多個集合

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

相關推薦

推薦中...