厲害了!每30秒學(xué)會(huì)一個(gè)Python小技巧,Github星數(shù)4600+
很多學(xué)習(xí)Python的朋友在項(xiàng)目實(shí)戰(zhàn)中會(huì)遇到不少功能實(shí)現(xiàn)上的問(wèn)題,有些問(wèn)題并不是很難的問(wèn)題,或者已經(jīng)有了很好的方法來(lái)解決。當(dāng)然,孰能生巧,當(dāng)我們代碼熟練了,自然就能總結(jié)一些好用的技巧,不過(guò)對(duì)于那些還在剛熟悉Python的同學(xué)可能并不會(huì)那么輕松。
本次給大家推薦一個(gè)學(xué)習(xí)這些技巧的很好的資源“30-seconds-of-python”,所有技巧方法只要30秒就能get到,完全可以利用業(yè)務(wù)時(shí)間不斷積累。下面趕緊來(lái)看一下。
https://github.com/30-seconds...
內(nèi)容目錄
下面是30秒學(xué)Python的整個(gè)目錄,分為幾大板塊:List、Math、Object、String、Utility,以下是整理的思維腦圖。
我挑選了10個(gè)實(shí)用并很有意思的方法分享給大家,其余的感興趣可以自行學(xué)習(xí)。
1. List:all_equal
功能實(shí)現(xiàn):檢驗(yàn)一個(gè)列表中的所有元素是否都一樣。
解讀:使用[1:] 和 [:-1] 來(lái)比較給定列表的所有元素。
- def all_equal(lst):
- return lst[1:] == lst[:-1]
舉例:
- all_equal([1, 2, 3, 4, 5, 6]) # False
- all_equal([1, 1, 1, 1]) # True
2. List:all_unique
功能實(shí)現(xiàn):如果列表所有值都是唯一的,返回 True,否則 False
解讀:在給定列表上使用集合set()去重,比較它和原列表的長(zhǎng)度。
- def all_unique(lst):
- return len(lst) == len(set(lst))
舉例:
- x = [1,2,3,4,5,6]
- y = [1,2,2,3,4,5]
- all_unique(x) # True
- all_unique(y) # False
3. List:bifurcate
功能實(shí)現(xiàn):將列表值分組。如果在filter的元素是True,那么對(duì)應(yīng)的元素屬于第一個(gè)組;否則屬于第二個(gè)組。
解讀:使用列表推導(dǎo)式和enumerate()基于filter元素到各組。
- def bifurcate(lst, filter):
- return [
- [x for i,x in enumerate(lst) if filter[i] == True],
- [x for i,x in enumerate(lst) if filter[i] == False]
- ]
舉例:
- bifurcate(['beep', 'boop', 'foo', 'bar'], [True, True, False, True])
- # [ ['beep', 'boop', 'bar'], ['foo'] ]
4. List:difference
功能實(shí)現(xiàn):返回兩個(gè)iterables間的差異。
解讀:創(chuàng)建b的集合,使用a的列表推導(dǎo)式保留不在_b中的元素。
- def difference(a, b):
- _b = set(b)
- return [item for item in a if item not in _b]
舉例:
- difference([1, 2, 3], [1, 2, 4]) # [3]
5. List:flatten
功能實(shí)現(xiàn):一次性的整合列表。
解讀:使用嵌套的列表提取子列表的每個(gè)值。
- def flatten(lst):
- return [x for y in lst for x in y]
舉例:
- flatten([[1,2,3,4],[5,6,7,8]]) # [1, 2, 3, 4, 5, 6, 7, 8]
6. Math:digitize
功能實(shí)現(xiàn):將一個(gè)數(shù)分解轉(zhuǎn)換為個(gè)位數(shù)字。
解讀:將n字符化后使用map()函數(shù)結(jié)合int完成轉(zhuǎn)化
- def digitize(n):
- return list(map(int, str(n)))
舉例:
- digitize(123) # [1, 2, 3]
7. List:shuffle
功能實(shí)現(xiàn):將列表元素順序隨機(jī)打亂。
解讀:使用Fisher-Yates算法重新排序列表元素。
- from copy import deepcopy
- from random import randint
- def shuffle(lst):
- temp_lst = deepcopy(lst)
- m = len(temp_lst)
- while (m):
- m -= 1
- i = randint(0, m)
- temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
- return temp_lst
舉例:
- foo = [1,2,3]
- shuffle(foo) # [2,3,1] , foo = [1,2,3]
8. Math:clamp_number
功能實(shí)現(xiàn):將數(shù)字num鉗在由a和b邊界值規(guī)定的范圍中。
解讀:如果num落盡范圍內(nèi),返回num;否則,返回范圍內(nèi)最接近的數(shù)字。
- def clamp_number(num,a,b):
- return max(min(num, max(a,b)),min(a,b))
舉例:
- clamp_number(2, 3, 5) # 3
- clamp_number(1, -1, -5) # -1
9. String:byte_size
功能實(shí)現(xiàn):返回字符串的字節(jié)數(shù)。
解讀:使用string.encode('utf-8')解碼給定字符串,返回長(zhǎng)度。
- def byte_size(string):
- return len(string.encode('utf-8'))
舉例:
- byte_size('?') # 4
- byte_size('Hello World') # 11
10. Math:gcd
功能實(shí)現(xiàn):計(jì)算幾個(gè)數(shù)的最大公因數(shù)。
解讀:使用reduce()和math.gcd在給定列表上實(shí)現(xiàn)。
- from functools import reduce
- import math
- def gcd(numbers):
- return reduce(math.gcd, numbers)
舉例:
- gcd([8,36,28]) # 4
以上就是30秒學(xué)python的各種小技巧。怎么樣,對(duì)于一些常見(jiàn)操作是不是有了一些新的啟發(fā),除此之外,還有很多其它技巧可以慢慢學(xué)習(xí),希望對(duì)各位讀者有所幫助。
https://github.com/30-seconds...