這42個Python小例子,太走心~

2021-12-23 程式設計師zhenguo

告別枯燥,60秒學會一個Python小例子。奔著此出發點,我在過去1個月,將平時經常使用的代碼段換為小例子,分享出來後受到大家的喜歡。

今天我完整梳理一遍,總結到這裡。很感謝這段時間,有3個小夥伴為此庫所做出的貢獻。

希望更多朋友能分享平日使用的小例子,它們包括但不限於:Python基礎、Web開發、數據科學、機器學習的精簡小例子。歡迎關注官方公眾號:

Python小例子

一、基本操作

1 鏈式比較

i = 3
print(1 < i < 3)  
print(1 < i <= 3)  

2 不用else和if實現計算器

from operator import *

def calculator(a, b, k):
    return {
        '+': add,
        '-': sub,
        '*': mul,
        '/': truediv,
        '**': pow
    }[k](a, b)

calculator(1, 2, '+')  
calculator(3, 4, '**')  

3 函數鏈

from operator import (add, sub)

def add_or_sub(a, b, oper):
    return (add if oper == '+' else sub)(a, b)

add_or_sub(1, 2, '-')  

4 求字符串的字節長度

def str_byte_len(mystr):
    return (len(mystr.encode('utf-8')))

str_byte_len('i love python')  
str_byte_len('字符')  

5 尋找第n次出現位置

def search_n(s, c, n):
    size = 0
    for i, x in enumerate(s):
        if x == c:
            size += 1
        if size == n:
            return i
    return -1

print(search_n("fdasadfadf", "a", 3))
print(search_n("fdasadfadf", "a", 30))

6 去掉最高最低求平均

def score_mean(lst):
    lst.sort()
    lst2=lst[1:(len(lst)-1)]
    return round((sum(lst2)/len(lst2)),2)

score_mean([9.1, 9.0,8.1, 9.7, 19,8.2, 8.6,9.8]) 

7 交換元素

def swap(a, b):
    return b, a

swap(1, 0)  

二、基礎算法

1 二分搜索

def binarySearch(arr, left, right, x):
    while left <= right:
        mid = int(left + (right - left) / 2); 

        
        if arr[mid] == x:
            print('found %d 在索引位置%d 處' %(x,mid))
            return mid

            
        elif arr[mid] < x:
            left = mid + 1 
            print('區間縮小為[%d,%d]' %(mid+1,right))

        elif x<arr[mid]:
            right = mid - 1 
            print('區間縮小為[%d,%d]' %(left,mid-1))

    return -1

2  距離矩陣

x,y = mgrid[0:5,0:5]
list(map(lambda xe,ye: [(ex,ey) for ex, ey in zip(xe, ye)], x,y))
[[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4)],
 [(1, 0), (1, 1), (1, 2), (1, 3), (1, 4)],
 [(2, 0), (2, 1), (2, 2), (2, 3), (2, 4)],
 [(3, 0), (3, 1), (3, 2), (3, 3), (3, 4)],
 [(4, 0), (4, 1), (4, 2), (4, 3), (4, 4)]]

三、列表

1 列印乘法表

for i in range(1,10):
    for j in range(1,i+1):
        print('{0}*{1}={2}'.format(j,i,j*i),end="\t")
    print()

結果:

1*1=1
1*2=2   2*2=4
1*3=3   2*3=6   3*3=9
1*4=4   2*4=8   3*4=12  4*4=16
1*5=5   2*5=10  3*5=15  4*5=20  5*5=25
1*6=6   2*6=12  3*6=18  4*6=24  5*6=30  6*6=36
1*7=7   2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49
1*8=8   2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64
1*9=9   2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81

2 嵌套數組完全展開

from collections.abc import *

def flatten(input_arr, output_arr=None):
    if output_arr is None:
        output_arr = []
    for ele in input_arr:
        if isinstance(ele, Iterable): 
            flatten(ele, output_arr)  
        else:
            output_arr.append(ele)    
    return output_arr

flatten([[1,2,3],[4,5]], [6,7]) 

3 將list等分為子組

from math import ceil

def divide(lst, size):
    if size <= 0:
        return [lst]
    return [lst[i * size:(i+1)*size] for i in range(0, ceil(len(lst) / size))]

r = divide([1, 3, 5, 7, 9], 2) 

4 生成fibonacci序列前n項

def fibonacci(n):
    if n <= 1:
        return [1]
    fib = [1, 1]
    while len(fib) < n:
        fib.append(fib[len(fib) - 1] + fib[len(fib) - 2])
    return fib

fibonacci(5)  

5 過濾掉各種空值

def filter_false(lst):
    return list(filter(bool, lst))

filter_false([None, 0, False, '', [], 'ok', [1, 2]])

6 返回列表頭元素

def head(lst):
    return lst[0] if len(lst) > 0 else None

head([])  
head([3, 4, 1])  

7 返回列表尾元素

def tail(lst):
    return lst[-1] if len(lst) > 0 else None

print(tail([]))  
print(tail([3, 4, 1]))  

8 對象轉換為可迭代類型

from collections.abc import Iterable

def cast_iterable(val):
    return val if isinstance(val, Iterable) else [val]

cast_iterable('foo')
cast_iterable(12)
cast_iterable({'foo': 12})

9 求更長列表

def max_length(*lst):
    return max(*lst, key=lambda v: len(v))

r = max_length([1, 2, 3], [4, 5, 6, 7], [8])

10 出現最多元素

def max_frequency(lst):
    return max(lst, default='列表為空', key=lambda v: lst.count(v))

lst = [1, 3, 3, 2, 1, 1, 2]
max_frequency(lst) 

11 求多個列表的最大值

def max_lists(*lst):
    return max(max(*lst, key=lambda v: max(v)))

max_lists([1, 2, 3], [6, 7, 8], [4, 5]) 

12 求多個列表的最小值

def min_lists(*lst):
    return min(min(*lst, key=lambda v: max(v)))

min_lists([1, 2, 3], [6, 7, 8], [4, 5]) 

13 檢查list是否有重複元素

def has_duplicates(lst):
    return len(lst) == len(set(lst))

x = [1, 1, 2, 2, 3, 2, 3, 4, 5, 6]
y = [1, 2, 3, 4, 5]
has_duplicates(x)  
has_duplicates(y)  

14 求列表中所有重複元素

from collections import Counter

def find_all_duplicates(lst):
    c = Counter(lst)
    return list(filter(lambda k: c[k] > 1, c))

find_all_duplicates([1, 2, 2, 3, 3, 3])  

15 列表反轉

def reverse(lst):
    return lst[::-1]

reverse([1, -2, 3, 4, 1, 2])

16 浮點數等差數列

def rang(start, stop, n):
    start,stop,n = float('%.2f' % start), float('%.2f' % stop),int('%.d' % n)
    step = (stop-start)/n
    lst = [start]
    while n > 0:
        start,n = start+step,n-1
        lst.append(round((start), 2))
    return lst

rang(1, 8, 10) 

四、字典

1 字典值最大的鍵值對列表

def max_pairs(dic):
    if len(dic) == 0:
        return dic
    max_val = max(map(lambda v: v[1], dic.items()))
    return [item for item in dic.items() if item[1] == max_val]

max_pairs({'a': -10, 'b': 5, 'c': 3, 'd': 5})

2 字典值最小的鍵值對列表

def min_pairs(dic):
    if len(dic) == 0:
        return []
    min_val = min(map(lambda v: v[1], dic.items()))
    return [item for item in dic.items() if item[1] == min_val]


min_pairs({}) 

r = min_pairs({'a': -10, 'b': 5, 'c': 3, 'd': 5})
print(r)  

3 合併兩個字典

def merge_dict2(dic1, dic2):
    return {**dic1, **dic2}  

merge_dict({'a': 1, 'b': 2}, {'c': 3})  

4 求字典前n個最大值

from heapq import nlargest


def topn_dict(d, n):
    return nlargest(n, d, key=lambda k: d[k])

topn_dict({'a': 10, 'b': 8, 'c': 9, 'd': 10}, 3)  

5 求最小鍵值對

d={'a':-10,'b':5, 'c':3,'d':5}
min(d.items(),key=lambda x:x[1]) 

五、集合

1 互為變位詞

from collections import Counter

def anagram(str1, str2):
    return Counter(str1) == Counter(str2)

anagram('eleven+two', 'twelve+one')  
anagram('eleven', 'twelve')  

六、文件操作

1 查找指定文件格式文件

import os

def find_file(work_dir,extension='jpg'):
    lst = []
    for filename in os.listdir(work_dir):
        print(filename)
        splits = os.path.splitext(filename)
        ext = splits[1] 
        if ext == '.'+extension:
            lst.append(filename)
    return lst

find_file('.','md') 

七、正則和爬蟲

1 爬取天氣數據並解析溫度值

素材來自朋友袁紹

import requests
from lxml import etree
import pandas as pd
import re

url = 'http://www.weather.com.cn/weather1d/101010100.shtml#input'
with requests.get(url) as res:
    content = res.content
    html = etree.HTML(content)

通過lxml模塊提取值,lxml比beautifulsoup解析在某些場合更高效

location = html.xpath('//*[@id="around"]//a[@target="_blank"]/span/text()')
temperature = html.xpath('//*[@id="around"]/div/ul/li/a/i/text()')

結果:

['香河', '涿州', '唐山', '滄州', '天津', '廊坊', '太原', '石家莊', '涿鹿', '張家口', '保定', '三河', '北京孔廟', '北京國子監', '中國地質博物館', '月壇公
園', '明城牆遺址公園', '北京市規劃展覽館', '什剎海', '南鑼鼓巷', '天壇公園', '北海公園', '景山公園', '北京海洋館']

['11/-5°C', '14/-5°C', '12/-6°C', '12/-5°C', '11/-1°C', '11/-5°C', '8/-7°C', '13/-2°C', '8/-6°C', '5/-9°C', '14/-6°C', '11/-4°C', '13/-3°C'
, '13/-3°C', '12/-3°C', '12/-3°C', '13/-3°C', '12/-2°C', '12/-3°C', '13/-3°C', '12/-2°C', '12/-2°C', '12/-2°C', '12/-3°C']

df = pd.DataFrame({'location':location, 'temperature':temperature})
print('溫度列')
print(df['temperature'])

正則解析溫度值

df['high'] = df['temperature'].apply(lambda x: int(re.match('(-?[0-9]*?)/-?[0-9]*?°C', x).group(1) ) )
df['low'] = df['temperature'].apply(lambda x: int(re.match('-?[0-9]*?/(-?[0-9]*?)°C', x).group(1) ) )
print(df)

詳細說明子字符創捕獲

除了簡單地判斷是否匹配之外,正則表達式還有提取子串的強大功能。用()表示的就是要提取的分組(group)。比如:^(\d{3})-(\d{3,8})$分別定義了兩個組,可以直接從匹配的字符串中提取出區號和本地號碼

m = re.match(r'^(\d{3})-(\d{3,8})$', '010-12345')
print(m.group(0))
print(m.group(1))
print(m.group(2))




如果正則表達式中定義了組,就可以在Match對象上用group()方法提取出子串來。

注意到group(0)永遠是原始字符串,group(1)、group(2)……表示第1、2、……個子串。

最終結果

Name: temperature, dtype: object
    location temperature  high  low
0         香河     11/-5°C    11   -5
1         涿州     14/-5°C    14   -5
2         唐山     12/-6°C    12   -6
3         滄州     12/-5°C    12   -5
4         天津     11/-1°C    11   -1
5         廊坊     11/-5°C    11   -5
6         太原      8/-7°C     8   -7
7        石家莊     13/-2°C    13   -2
8         涿鹿      8/-6°C     8   -6
9        張家口      5/-9°C     5   -9
10        保定     14/-6°C    14   -6
11        三河     11/-4°C    11   -4
12      北京孔廟     13/-3°C    13   -3
13     北京國子監     13/-3°C    13   -3
14   中國地質博物館     12/-3°C    12   -3
15      月壇公園     12/-3°C    12   -3
16   明城牆遺址公園     13/-3°C    13   -3
17  北京市規劃展覽館     12/-2°C    12   -2
18       什剎海     12/-3°C    12   -3
19      南鑼鼓巷     13/-3°C    13   -3
20      天壇公園     12/-2°C    12   -2
21      北海公園     12/-2°C    12   -2
22      景山公園     12/-2°C    12   -2
23     北京海洋館     12/-3°C    12   -3

2 批量轉化駝峰格式

import re
def camel(s):
    s = re.sub(r"(\s|_|-)+", " ", s).title().replace(" ", "")
    return s[0].lower() + s[1:]


def batch_camel(slist):
    return [camel(s) for s in slist]

batch_camel(['student_id', 'student\tname', 'student-add']) 

八、繪圖

1 turtle繪製奧運五環圖
結果:

2 turtle繪製漫天雪花
結果:

3 4種不同顏色的色塊,它們的顏色真的不同嗎?

4 詞頻雲圖

import hashlib
import pandas as pd
from wordcloud import WordCloud
geo_data=pd.read_excel(r"../data/geo_data.xlsx")
words = ','.join(x for x in geo_data['city'] if x != []) 
wc = WordCloud(
    background_color="green", 
    max_words=100, 
    font_path='./fonts/simhei.ttf', 
    min_font_size=5,
    max_font_size=100,
    width=500  
    )
x = wc.generate(words)
x.to_file('../data/geo_data.png')


八、生成器

1 求斐波那契數列前n項(生成器版)

def fibonacci(n):
    a, b = 1, 1
    for _ in range(n):
        yield a
        a, b = b, a + b

list(fibonacci(5))  

2 將list等分為子組(生成器版)

from math import ceil

def divide_iter(lst, n):
    if n <= 0:
        yield lst
        return
    i, div = 0, ceil(len(lst) / n)
    while i < n:
        yield lst[i * div: (i + 1) * div]
        i += 1

list(divide_iter([1, 2, 3, 4, 5], 0))  
list(divide_iter([1, 2, 3, 4, 5], 2))  

九、keras

1 Keras入門例子

import numpy as np
from keras.models import Sequential
from keras.layers import Dense

data = np.random.random((1000, 1000))
labels = np.random.randint(2, size=(1000, 1))
model = Sequential()
model.add(Dense(32,
                activation='relu',
                input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimize='rmsprop', loss='binary_crossentropy',
              metrics=['accuracy'])
model.fit(data, labels, epochs=10, batch_size=32)
predictions = model.predict(data)

歡迎關注和貢獻

要想查看更多小例子,歡迎關注這個Python小例子庫:

https://github.com/jackzhenguo/python-small-examples

由於微信公眾號中無法直接點擊外部連結,所以想star此庫的朋友可點擊文章最底部的【閱讀原文】,直達github此庫界面。

比如github帳號為lhxon的小夥伴,fork此庫後,按照如下步驟提交到此庫:

1. git clone https://github.com/lhxon/python-small-examples
2. git add . 
3. git commit -m "xiugai"
4. git push
5. 界面點擊:pull requests,根據操作即可。如遇問題,歡迎聯繫我。

相關焦點

  • 42個Python小例子
    告別枯燥,60秒學會一個Python小
  • 223個Python小例子(1-60)
    文末閱讀原文: 獲取2020年,最強Python學習資料Python小例子貢獻歡迎貢獻小例子到此庫小例子有的夥伴可能會想這還不簡單,這樣寫:i = 5def f(): print(i)def g(): print(i) passf()g()f和g兩個函數都能共享變量i,程序沒有報錯,所以他們依然不明白為什麼要用global.
  • Python要點總結,我使用了100個小例子!
    ]: Counter(skuPurchaseCount).most_common()Out[42]:[(3, 5),(1, 4),(7, 3),(0, 2),(8, 1),(10, 1),(6, 1),(2, 1),(9, 1),(5, 1)]僅僅一行代碼,我們便輸出統計計數結果,並且是一個按照次數統計出來的由大到小排序好的tuples列表,因此我們很快就會看到
  • 20多個python實用小例子(1)
    我整理了 20多 個Python入門的小例子。方便同學們在實踐中能夠依葫蘆畫瓢,慢慢習慣使用python。如何加入python交流和答疑群:請需要的同學掃描下面二維碼,通過後會拉你進入交流群,請注意:關注公眾號。
  • 【python技能】Python要點總結,30個小例子剖析難易度,贈送海量學習福利
    這也是很多人擔心的問題吧!其實大可不必擔心,這是我在職友集上隨手搜索的Python開發工作的招聘信息。打開搜索欄,搜索Python出來的招聘信息足可讓你挑花了眼。很快,你將會意識到數百個特徵位於此list 中,這就是事情變得糟糕的開始。
  • 一個go和python有趣的例子
    今天在一博文中看到一個有趣的問題,分享給大家,曾經也通過自學學習過python程式語言,python是一門很簡煉、功能豐富的語言,目前已大量用於科學研究和網際網路開發各個領域
  • 60 個 Python 闖關小例子,建議收藏
    有的夥伴可能會想這還不簡單,這樣寫:i = 5def f(): print(i)def g(): print(i) passf()g()f和g兩個函數都能共享變量i,程序沒有報錯,所以他們依然不明白為什麼要用global.
  • 【必收藏系列】60個Python小例子
    有的夥伴可能會想這還不簡單,這樣寫:i = 5def f(): print(i)def g(): print(i) passf()g()f和g兩個函數都能共享變量i,程序沒有報錯,所以他們依然不明白為什麼要用global.
  • 60 個 Python 闖關小例子,建議收藏!
    有的夥伴可能會想這還不簡單,這樣寫:i = 5def f(): print(i)def g(): print(i) passf()g()f和g兩個函數都能共享變量i,程序沒有報錯,所以他們依然不明白為什麼要用global.
  • Python之路200個小例子,在線網頁版來了,從此學習更方便!
    歷史兩個月,利用所有業餘時間,與朋友一起搜集、創作Python小例子,截止目前已超過200個例子,全新整合匯總為九大章節
  • python操作gff格式注釋文件的簡單小例子
    這裡藉助biopython模塊參考連結是 https://biopython.org/wiki/GFF_Parsing這裡BCBio模塊裡GFF()函數解析的內容和Bio模塊裡SeqIO()函數解析的內容很像cds和外顯子的關係
  • Python 實用的小例子分享 1~3
    例子卡片1:批量修改文件後綴 本例子使用Python的os模塊和 argparse模塊,將工作目錄work_dir下所有後綴名為old_ext的文件修改為後綴名為new_ext通過本例子,大家將會大概清楚argparse模塊的主要用法。
  • 絕對收藏的Python小例子(上)
    有的夥伴可能會想這還不簡單,這樣寫:i = 5def f(): print(i)def g(): print(i) passf()g()f 和 g 兩個函數都能共享變量 i,程序沒有報錯,所以他們依然不明白為什麼要用 global.
  • 慢步學python,編程基礎,字符串類型例子及輸出
    但python理解為'I' 和後面部分代碼m Hero!' 因為這裡由單引號開始,python會將第2個單引號理解為字符串的結束。在單引號字符串想要顯示單引號,慢步用轉義字符\ 反斜槓 才成功輸出。轉義字符\ 的作用就是在特殊字符前使用,可以實現特殊的顯示功能。
  • 60個Python實用小例子,建議收藏(文末送書)
    🚀本文推出60個Python實用小例子,建議收藏,方便後期學習和回顧用。python五類參數:位置參數,關鍵字參數,默認參數,可變位置或關鍵字參數的使用。藉助python的inspect模塊:In [22]: for name,val in signature(f).parameters.items(): ...: print(name,val.kind) ...
  • 掌握Python字典的12個例子
    這些例子將涵蓋字典的特性,以及對它們進行操作的函數和方法。1.創建字典我們可以通過在大括號之間提供0個或多個鍵值對來創建字典。舉個例子會更清楚。好處:使用python3.9合併和更新操作符Python3.9為字典提供了merge(「|」)和update(「|=」)運算符。
  • Python多進程:有例子的教程
    同時,注意 Larry Wall提出的三個編程美德,特別是:焦躁:當電腦變得懶惰的時候,你感到憤怒。這讓你寫的程序不只是對你的需求作出反應,實際上還預期電腦作出反應,或者至少假裝作出有反應。我們使用默認的調度策略,即「最小負載」策略,來開啟集群。對於其他的調度策略,我們可以使用下面的指令來尋找幫助:
  • 20多個python實用小例子(23)--如何對數值進行精確的小數計算
    比如下面的例子:Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32Type "copyright", "credits" or "license()" for
  • 100 個 Python 小例子
    原題地址:http://www.runoob.com/python/python-100-examples.html,原例為py2.7版本,重寫過程中有不少是隨意發揮的,重寫運行版本:Python3.7。
  • ​【Python基礎】告別枯燥,60 秒學會一個 Python 小例子
    本文推薦一個python的傻瓜式的學習資源,內容簡單易懂,讓人可以在60 秒學會一個 Python 小例子當前庫已有 300多 個實用的小例子本文來源:https://github.com/jackzhenguo/python-small-examples文章轉自:機器學習初學者本庫目錄