banner
andrewji8

Being towards death

Heed not to the tree-rustling and leaf-lashing rain, Why not stroll along, whistle and sing under its rein. Lighter and better suited than horses are straw sandals and a bamboo staff, Who's afraid? A palm-leaf plaited cape provides enough to misty weather in life sustain. A thorny spring breeze sobers up the spirit, I feel a slight chill, The setting sun over the mountain offers greetings still. Looking back over the bleak passage survived, The return in time Shall not be affected by windswept rain or shine.
telegram
twitter
github

python的北冥神功62式

image

北冥神功,取名於道家的莊子《逍遙遊》。它的主旨要訣全在以下幾點:

“世人練功,皆自雲門而至少商,我逍遙派則反其道而行之,自少商而至雲門,拇指與人相接,彼之內力即入我身,儲於雲門等諸穴。然敵之內力若勝於我,則海水倒灌而入江河,凶險莫甚,慎之,慎之。

從北冥神功的修煉心法來看,它和普通內功修煉者的行走筋脈是完全相反的。因此北冥神功具有海納百川的功能,才可以做到天下內功全都可以吸取。而且這種吸取是完全沒有副作用,因為它還可以將別人的內力同化。

image

iterable 技巧

▍1、創建一個數字序列(從 0 到 10,間隔為 2)

>>> range(0,10,2)
[0, 2, 4, 6, 8]

▍2、對一串數字求和(從 0 到 10,間隔為 2)

>>> l = range(0,10,2)
>>> sum(l)
20

▍3、檢查序列中的任一元素是否為 True

>>> any(a % 2 for a in range(0,10,2))
True

▍4、檢查序列中的所有元素是否為 True

>>> all(a % 2 for a in range(0,10,2))
True

▍5、累計求和一串數字序列

>>> import numpy as np
>>> res = list(np.cumsum(range(0,10,2)))
>>> res
[ 0, 2, 6, 12, 20]

▍6、給定每個 iterable,通過添加索引來構造一個元組

>>> a = ['Hello', 'world', '!']
>>> list(enumerate(a))
[(0, 'Hello'), (1, 'world'), (2, '!')]

▍7、將 iterable 連接到單個字符串

>>> a = ["python","really", "rocks"]
>>> " ".join(a)
'python really rocks'

▍8、組合兩個可迭代的元組或 pivot 嵌套的 iterables

# Combining two iterables
>>> a = [1, 2, 3]
>>> b = ['a', 'b', 'c']
>>> z = zip(a, b)
>>> z
[(1, 'a'), (2, 'b'), (3, 'c')]

# Pivoting list of tuples
>>> zip(*z)
[(1, 2, 3), ('a', 'b', 'c')]

▍9、從 iterables 中獲取最小值 / 最大值(具有 / 不具有特定功能)

# Getting maximum from iterable
>>> a = [1, 2, -3]
>>> max(a)
2

# Getting maximum from iterable
>>> min(a)
1

# Bot min/max has key value to allow to get maximum by appliing function
>>> max(a,key=abs)
3

▍10、可迭代排序(可以通過 “compare” 函數排序)

>>> a = [1, 2, -3]
>>> sorted(a)
[-3, 1, 2]

>>> sorted(a,key=abs)
[1, 2, -3]

▍11、將單個字符串拆分為列表

>>> s = "a,b,c"
>>> s.split(",")
["a", "b", "c"]

▍12、初始化一個包含重複數字的列表

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

▍13、合併 / 插入兩個字典

>>> a = {"a":1, "b":1}
>>> b = {"b":2, "c":1}
>>> a.update(b)
>>> a
{"a":1, "b":2, "c":1}

▍14、命名和保存 iterables 切片

# Naming slices (slice(start, end, step))
>>> a = [0, 1, 2, 3, 4, 5]
>>> LASTTHREE = slice(-3, None)
>>> LASTTHREE
slice(-3, None, None)
>>> a[LASTTHREE]
[3, 4, 5]

▍15、在列表中查找項的索引

>>> a = ["foo", "bar", "baz"]
>>> a.index("bar")
1

▍16、在 iterables 中查找最小 / 最大項的索引

>>> a = [2, 3, 1]
>>> min(enumerate(a),key=lambda x: x[1])[0]
2

▍17、旋轉 iterables 的 k 個元素

>>> a = [1, 2, 3, 4]
>>> k = 2
>>> a[-2:] + a[:-2]
[3, 4, 1, 2]

▍18、刪除字符串末尾 / 開始 / 兩端無用的字符

>>> name = "//George//"
>>> name.strip("/")
'George'
>>> name.rstrip("/")
'//George'
>>> name.lstrip("/")
'George//'

▍19、倒序 iterables 的順序(字符串、列表等)

# Reversing string
>>> s = "abc"
>>> s[::-1]
"cba"

# Reversing list
>>> l = ["a", "b", "c"]
>>> l[::-1]
["c", "b", "a"]

branching 技巧

▍20、多個 short-cut

>>> n = 10
>>> 1 < n < 20
True

▍21、For-else 結構在搜索某些東西並找到它時很有用

for i in mylist:
    if i == theflag:
        break
    process(i)
else:
    raise ValueError("List argument missing terminal flag.")

▍22、Trenary operator

>>> "Python ROCK" if True else " I AM GRUMPY"
"Python ROCK"

▍23、Try-catch-else 結構

try:
    foo()
except Exception:
    print("Exception occured")
else:
    print("Exception didnt occur")
finally:
    print("Always gets here")

▍24、While-else 結構

i = 5

while i > 1:
    print("Whil-ing away!")
    i -= 1
    if i == 3:
        break
else:
    print("Finished up!")

comprehensions(推導式)技巧

▍25、List 推導式

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]

▍26、Set 推導式

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}

▍27、Dict 推導式

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

▍28、Generator 推導式

# A generator comprehension is the lazy version of a list comprehension.
>>> m = (x ** 2 for x in range(5))
>>> m
<generator object <genexpr> at 0x108efe408>
>>> list(m)
[0, 1, 4, 9, 16]

>>> m = (x ** 2 for x in range(5))
>>> next(m)
0
>>> list(m)
[1, 4, 9, 16]

▍29、list 推導使用當前值和過往值

>>> a = [1, 2, 4,2]
>>> [y - x for x,y in zip(a,a[1:])]
[1, 2, -2]

unpacking 技巧

▍30、從 iterable 解壓縮變量

# One can unpack all iterables (tuples, list etc)
>>> a, b, c = 1, 2, 3
>>> a, b, c
(1, 2, 3)

>>> a, b, c = [1, 2, 3]
>>> a, b, c
(1, 2, 3)

▍31、交換變量值

>>> a, b = 1, 2
>>> a, b = b, a
>>> a, b
(2, 1)

▍32、在不指示所有元素的情況下從 iterable 解包變量

>>> a, *b, c = [1, 2, 3, 4, 5]
>>> a
1
>>> b
[2, 3, 4]
>>> c
5

▍33、使用 splat 運算符解包變量

>>> def test(x, y, z):
>>>      print(x, y, z)
>>> res = test(*[10, 20, 30])
10 20 30
>>> res = test(**{'x': 1, 'y': 2, 'z': 3} )
10 20 30
view raw

Itertools 技巧

▍34、Flatten iterables

>>> a = [[1, 2], [3, 4], [5, 6]]
>>> list(itertools.chain.from_iterable(a))
[1, 2, 3, 4, 5, 6]

▍35、從 iterables 創建笛卡爾積

>>> for p in itertools.product([1, 2, 3], [4, 5]):
>>>      print(''.join(str(x) for x in p))

(1, 4)
(1, 5)
(2, 4)
(2, 5)
(3, 4)
(3, 5)

▍36、從 iterable 創建排列

>>> for p in itertools.permutations([1, 2, 3, 4]):
>>>      print(''.join(str(x) for x in p))
123
132
213
231
312
321

▍37、從 iterable 創建 ngram

>>> from itertools import islice
>>> def n_grams(a, n):
...          z = (islice(a, i, None) for i in range(n))
...          return zip(*z)
...
>>> a = [1, 2, 3, 4, 5, 6]
>>> n_grams(a, 3)
[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)]
>>> n_grams(a, 2)
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
>>> n_grams(a, 4)
[(1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6)]

▍38、使用填充組合元組的兩個迭代器或使用填充 pivot 嵌套迭代

>>> import itertools as it
>>> x = [1, 2, 3, 4, 5]
>>> y = ['a', 'b', 'c']
>>> list(zip(x, y))
[(1, 'a'), (2, 'b'), (3, 'c')]

>>> list(it.zip_longest(x, y))
[(1, 'a'), (2, 'b'), (3, 'c'), (4, None), (5, None)]

▍39、從一個 iterable n 中創建 k 個組合

>>> import itertools
>>> bills = [20, 20, 20, 10, 10, 10, 10, 10, 5, 5, 1, 1, 1, 1, 1]
>>> list(itertools.combinations(bills, 3))
[(20, 20, 20), (20, 20, 10), (20, 20, 10), ... ]

▍40、在給定函數情況下創建一個迭代的累積結果

>>> import itertools
>>> list(itertools.accumulate([9, 21, 17, 5, 11, 12, 2, 6], min))
[9, 9, 9, 5, 5, 5, 2, 2]

▍41、創建一個迭代器,只要謂詞為 True,就從 iterable 返回元素

>>> import itertools
>>> itertools.takewhile(lambda x: x < 3, [0, 1, 2, 3, 4])
[0, 1, 2]

>>> it.dropwhile(lambda x: x < 3, [0, 1, 2, 3, 4])
[3, 4]

▍42、創建一個迭代器,它從 iterable 中过濾元素,只返回謂詞為 False 的元素

>>> import itertools
# keeping only false values
>>> list(itertools.filterfalse(bool, [None, False, 1, 0, 10]))
[None, False, 0]

▍43、創建一個迭代器,使用從迭代的迭代中獲得的參數來計算函數

>>> import itertools
>>> import operator
>>> a = [(2, 6), (8, 4), (7, 3)]
>>> list(itertools.starmap(operator.mul, a))
[12, 32, 21]

collections 技巧

▍44、設置基本操作

>>> A = {1, 2, 3, 3}
>>> A
set([1, 2, 3])
>>> B = {3, 4, 5, 6, 7}
>>> B
set([3, 4, 5, 6, 7])
>>> A | B
set([1, 2, 3, 4, 5, 6, 7])
>>> A & B
set([3])
>>> A - B
set([1, 2])
>>> B - A
set([4, 5, 6, 7])
>>> A ^ B
set([1, 2, 4, 5, 6, 7])
>>> (A ^ B) == ((A - B) | (B - A))
True

▍45、計數器數據結構(無序集合,其中元素存儲為字典鍵,其計數存儲為字典值)

import collections

>>> A = collections.Counter([1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 7])
>>> A
Counter({3: 4, 1: 2, 2: 2, 4: 1, 5: 1, 6: 1, 7: 1})
>>> A.most_common(1)
[(3, 4)]
>>> A.most_common(3)
[(3, 4), (1, 2), (2, 2)]

▍46、默認字典結構(字典的子類,在訪問不存在的鍵時檢索默認值)

>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
[]

▍47、有序的 dict 結構(保持有序字典的子類)

>>> from collections import OrderedDict

>>> d = OrderedDict.fromkeys('abcde')
>>> d.move_to_end('b')
>>> ''.join(d.keys())
'acdeb'

>>> d.move_to_end('b', last=False)
>>> ''.join(d.keys())
'bacde'

▍48、Deques 結構(Deques 是堆棧和隊列的概括)

>>> import collection
>>> Q = collections.deque()
>>> Q.append(1)
>>> Q.appendleft(2)
>>> Q.extend([3, 4])
>>> Q.extendleft([5, 6])
>>> Q
deque([6, 5, 2, 1, 3, 4])
>>> Q.pop()
4
>>> Q.popleft()
6
>>> Q
deque([5, 2, 1, 3])
>>> Q.rotate(3)
>>> Q
deque([2, 1, 3, 5])
>>> Q.rotate(-3)
>>> Q
deque([5, 2, 1, 3])

>>> last_three = collections.deque(maxlen=3)
>>> for i in range(4):
...         last_three.append(i)
...         print ', '.join(str(x) for x in last_three)
...
0
0, 1
0, 1, 2
1, 2, 3
2, 3, 4

▍49、命名元組結構(創建類元組的對象,這些對象的字段可通過屬性查找訪問,也可索引和迭代)

>>> import collections
>>> Point = collections.namedtuple('Point', ['x', 'y'])
>>> p = Point(x=1.0, y=2.0)
>>> p
Point(x=1.0, y=2.0)
>>> p.x
1.0
>>> p.y
2.0

▍50、使用字典來存儲 Switch

>>> func_dict = {'sum': lambda x, y: x + y, 'subtract': lambda x, y: x - y}
>>> func_dict['sum'](9,3)
12
>>> func_dict['subtract'](9,3)
6

▍51、數據類結構

>>> from dataclasses import dataclass

>>> @dataclass
>>> class DataClassCard:
>>>     rank: str
>>>      suit: str

>>> queen_of_hearts = DataClassCard('Q', 'Hearts')
>>> queen_of_hearts.rank
'Q'
>>> queen_of_hearts
DataClassCard(rank='Q', suit='Hearts')
>>> queen_of_hearts == DataClassCard('Q', 'Hearts')
True

其他技巧

▍52、生成 uuid

# This creates a randomized
#128-bit number that will almost certainly be unique.
# In fact, there are over 2¹²² possible
#UUIDs that can be generated.
#That’s over five undecillion (or 5,000,000,000,000,000,000,000,000,000,000,000,000).

>>> import uuid
>>> user_id = uuid.uuid4()
>>> user_id
UUID('7c2faedd-805a-478e-bd6a-7b26210425c7')

▍53、使用 LRU 緩存進行記憶

import functools

@functools.lru_cache(maxsize=128)
def fibonacci(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    return fibonacci(n - 1) + fibonacci(n - 2)

▍54、Suppressing expressions

>>> from contextlib import suppress
>>> with contextlib.suppress(ZeroDivisionError):
>>> 10/0
# No exception raised

▍55、在需要設置和拆卸時創建上下文管理

class FileManager:
    def __init__(self, filename):
        self.filename = filename
        self.file = None

    def __enter__(self):
        self.file = open(self.filename, 'r')
        return self.file

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.file:
            self.file.close()

# 使用上下文管理器讀取文件
with FileManager('example.txt') as file:
    for line in file:
        print(line)

▍56、一種處理文件路徑的優雅方法(3.4≥)

>>> from pathlib import Path
>>> data_folder = Path("source_data/text_files/)

# Path calculation and metadata
>>> file_to_open = data_folder / "raw_data.txt"
>>> file_to_open.name
"raw_data.txt"
>>> file_to_open.suffix
"txt"
>>>file_to_open.stem
"raw_data"

# Files functions
>>> f = open(file_to_open)
>>> f.read()
# content of the file
>>> file_to_open.exists()
True

▍57、將標準操作符實現為類的函數

    @staticmethod
    def add(x, y):
        return x + y

    @staticmethod
    def subtract(x, y):
        return x - y

    @staticmethod
    def multiply(x, y):
        return x * y

    @staticmethod
    def divide(x, y):
        return x / y

▍58、創建裝飾器來分離 concerns

>>>from functools import wraps

>>>def add_sandwich(wrapped):
>>>      @wraps(wrapped)
>>>      def wrapper(*args, **kwargs):
>>>            return wrapped(*args, **kwargs) + ' sandwich'
>>>      return wrapper

>>>@add_sandwich
>>>def ham():
>>>     return 'ham'

>>>ham()
'ham sandwich'

▍59、使用 yield 創建一個簡單的迭代器

>>> def foo(lst):
>>>      for x in lst:
>>>          yield x
>>>          yield x*2

>>> a = [1, 3]
>>> list(foo(a))
[1, 2, 3, 6]

▍60、yield from use cases and tricks

def numbers():
    yield from range(1, 4)

def letters():
    yield from ('A', 'B', 'C')

def combine():
    yield from numbers()
    yield from letters()

for value in combine():
    print(value)
# 輸出結果:1 2 3 A B C

彩蛋

▍61、Anti-gravity

import antigravity

antigravity.fly()

▍62、The Zen of Python

>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren not special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one and preferably only one obvious way to do it.
Although that way may not be obvious at first unless you are Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it is a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea let us do more of those!
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。