身為暢銷書作家,我邀請您在亞馬遜上探索我的書。不要忘記在 Medium 上關注我並表示您的支持。謝謝你!您的支持意味著全世界!
身為一個在大數據處理方面擁有豐富經驗的Python開發人員,我發現生成器是高效處理大型資料集不可或缺的工具。在本文中,我將分享五種強大的生成器技術,這些技術顯著地改善了我的資料處理工作流程。
生成器表達式是 Python 中記憶體高效資料處理的基石。與在記憶體中建立整個清單的清單推導式不同,生成器表達式會按需產生值。這種方法在處理大型資料集時特別有用。
考慮這個例子,我們需要處理一個大的 CSV 檔案:
def csv_reader(file_path): with open(file_path, 'r') as file: for line in file: yield line.strip().split(',') def process_large_csv(file_path): data_gen = csv_reader(file_path) processed_gen = (process_row(row) for row in data_gen) for processed_row in processed_gen: # Further processing or storage pass
在此程式碼中,我們使用生成器函數 csv_reader 從 CSV 檔案中一次產生一行。然後,我們使用生成器表達式來處理每一行。這種方法允許我們處理任何大小的文件,而無需將整個資料集載入到記憶體中。
yield from 語句是扁平化巢狀產生器的強大工具。它簡化了程式碼並提高了處理複雜資料結構時的效能。
這是使用yield from處理巢狀JSON資料的範例:
import json def flatten_json(data): if isinstance(data, dict): for key, value in data.items(): yield from flatten_json(value) elif isinstance(data, list): for item in data: yield from flatten_json(item) else: yield data def process_large_json(file_path): with open(file_path, 'r') as file: data = json.load(file) for item in flatten_json(data): # Process each flattened item pass
這段程式碼有效地扁平化了巢狀的 JSON 結構,使我們能夠處理複雜的資料而無需建立中間列表。
無限生成器對於建立資料流或模擬連續過程特別有用。它們可以用於我們需要無限期地產生資料或直到滿足特定條件為止的場景。
這是模擬感測器資料的無限生成器的範例:
import random import time def sensor_data_generator(): while True: yield { 'timestamp': time.time(), 'temperature': random.uniform(20, 30), 'humidity': random.uniform(40, 60) } def process_sensor_data(duration): start_time = time.time() for data in sensor_data_generator(): print(f"Temperature: {data['temperature']:.2f}°C, Humidity: {data['humidity']:.2f}%") if time.time() - start_time > duration: break time.sleep(1) process_sensor_data(10) # Process data for 10 seconds
這個無限發生器不斷產生類比感測器資料。 process_sensor_data 函數使用此產生器來處理指定持續時間的資料。
生成器管道是建立複雜資料轉換鏈的一種優雅方式。管道中的每個步驟都可以是一個生成器,從而可以有效地處理大型資料集。
這是用於處理日誌檔案的生成器管道的範例:
import re def read_logs(file_path): with open(file_path, 'r') as file: for line in file: yield line.strip() def parse_logs(lines): pattern = r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) \[(\w+)\] (.+)' for line in lines: match = re.match(pattern, line) if match: yield { 'timestamp': match.group(1), 'level': match.group(2), 'message': match.group(3) } def filter_errors(logs): for log in logs: if log['level'] == 'ERROR': yield log def process_log_file(file_path): logs = read_logs(file_path) parsed_logs = parse_logs(logs) error_logs = filter_errors(parsed_logs) for error in error_logs: print(f"Error at {error['timestamp']}: {error['message']}") process_log_file('application.log')
該管道讀取日誌文件,解析每一行,過濾錯誤訊息並處理它們。每個步驟都是一個生成器,可以有效率地處理大型日誌檔案。
Python 中的 itertools 模組提供了一組快速、節省記憶體的工具來使用迭代器。這些函數在處理生成器輸出時特別有用。
這是一個使用 itertools.islice 和 itertools.groupby 處理大型資料集的範例:
def csv_reader(file_path): with open(file_path, 'r') as file: for line in file: yield line.strip().split(',') def process_large_csv(file_path): data_gen = csv_reader(file_path) processed_gen = (process_row(row) for row in data_gen) for processed_row in processed_gen: # Further processing or storage pass
在此範例中,我們使用 islice 來限制處理的項目數量,並使用 groupby 按類別將資料分組。這種方法使我們能夠有效地處理和分析大型資料集的子集。
使用生成器時,正確的錯誤處理至關重要。由於生成器可能會耗盡,因此我們需要處理潛在的 StopIteration 異常以及處理過程中可能發生的其他錯誤。
這是基於生成器的資料處理管道中穩健錯誤處理的範例:
import json def flatten_json(data): if isinstance(data, dict): for key, value in data.items(): yield from flatten_json(value) elif isinstance(data, list): for item in data: yield from flatten_json(item) else: yield data def process_large_json(file_path): with open(file_path, 'r') as file: data = json.load(file) for item in flatten_json(data): # Process each flattened item pass
此程式碼示範如何處理專案層級和生成器層級的錯誤,確保大型資料集的穩健處理。
要在使用生成器時最佳化效能,請考慮以下提示:
- 盡可能使用生成器表達式而不是列表推導式。
- 在生成器內實現昂貴計算的快取。
- 使用 itertools 模組進行高效率的迭代器操作。
- 考慮使用多重處理來並行處理 CPU 密集型任務。
這是在生成器中實作快取的範例:
import random import time def sensor_data_generator(): while True: yield { 'timestamp': time.time(), 'temperature': random.uniform(20, 30), 'humidity': random.uniform(40, 60) } def process_sensor_data(duration): start_time = time.time() for data in sensor_data_generator(): print(f"Temperature: {data['temperature']:.2f}°C, Humidity: {data['humidity']:.2f}%") if time.time() - start_time > duration: break time.sleep(1) process_sensor_data(10) # Process data for 10 seconds
此程式碼使用 lru_cache 裝飾器來快取昂貴的計算結果,顯著提高重複值的效能。
產生器對於處理大型日誌檔案特別有用。下面是一個更進階的範例,示範如何處理 Apache 存取日誌:
import re def read_logs(file_path): with open(file_path, 'r') as file: for line in file: yield line.strip() def parse_logs(lines): pattern = r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) \[(\w+)\] (.+)' for line in lines: match = re.match(pattern, line) if match: yield { 'timestamp': match.group(1), 'level': match.group(2), 'message': match.group(3) } def filter_errors(logs): for log in logs: if log['level'] == 'ERROR': yield log def process_log_file(file_path): logs = read_logs(file_path) parsed_logs = parse_logs(logs) error_logs = filter_errors(parsed_logs) for error in error_logs: print(f"Error at {error['timestamp']}: {error['message']}") process_log_file('application.log')
此程式碼有效處理大型 Apache 存取日誌文件,提供有關 IP 位址頻率、狀態代碼分佈和傳輸資料總量的見解。
在處理大型 XML 文件時,生成器特別有幫助。以下是使用 xml.etree.ElementTree 模組處理大型 XML 檔案的範例:
import itertools def large_dataset(): for i in range(1000000): yield {'id': i, 'category': chr(65 + i % 26), 'value': i * 2} def process_data(): data = large_dataset() # Process only the first 100 items first_100 = itertools.islice(data, 100) # Group the first 100 items by category grouped = itertools.groupby(first_100, key=lambda x: x['category']) for category, items in grouped: print(f"Category {category}:") for item in items: print(f" ID: {item['id']}, Value: {item['value']}") process_data()
此程式碼使用 iterparse 有效地處理大型 XML 文件,而無需將整個文件載入記憶體。它會產生具有特定標籤名稱的元素,允許對大型 XML 結構進行有針對性的處理。
產生器也非常適合在 ETL(提取、轉換、載入)過程中實現資料管道。以下是使用生成器的簡單 ETL 管道的範例:
def safe_process(generator): try: for item in generator: try: yield process_item(item) except ValueError as e: print(f"Error processing item: {e}") except StopIteration: print("Generator exhausted") except Exception as e: print(f"Unexpected error: {e}") def process_item(item): # Simulate processing that might raise an error if item % 10 == 0: raise ValueError("Invalid item") return item * 2 def item_generator(): for i in range(100): yield i for result in safe_process(item_generator()): print(result)
此 ETL 管道從 CSV 檔案讀取數據,透過應用一些業務邏輯對其進行轉換,然後將其載入到 JSON 檔案中。使用生成器可以以最小的記憶體使用量高效處理大型資料集。
總之,Python 產生器是高效大數據處理的強大工具。它們使我們能夠處理大型資料集,而無需立即將所有內容載入到記憶體中。透過使用生成器表達式、yield from、無限生成器、生成器管道和 itertools 模組等技術,我們可以創建記憶體高效且高效能的資料處理工作流程。
在我的職業生涯中,我發現這些生成器技術在處理大量日誌檔案、複雜的 XML/JSON 文件和大規模 ETL 流程時非常寶貴。它們使我能夠處理傳統方法無法處理的數據。
當您使用 Python 處理大數據時,我鼓勵您探索這些生成器技術並將其合併到您的專案中。它們不僅可以提高程式碼的效率,還可以讓您輕鬆處理更大、更複雜的資料處理任務。
101 本書
101 Books是一家由人工智慧驅動的出版公司,由作家Aarav Joshi共同創立。透過利用先進的人工智慧技術,我們將出版成本保持在極低的水平——一些書籍的價格低至 4 美元——讓每個人都能獲得高品質的知識。
查看我們的書Golang Clean Code,亞馬遜上有售。
請繼續關注更新和令人興奮的消息。購買書籍時,搜尋 Aarav Joshi 以尋找更多我們的書籍。使用提供的連結即可享受特別折扣!
我們的創作
一定要看看我們的創作:
投資者中心 | 投資者中央西班牙語 | 投資者中德意志 | 智能生活 | 時代與迴響 | 令人費解的謎團 | 印度教 | 菁英發展 | JS學校
我們在媒體上
科技無尾熊洞察 | 時代與迴響世界 | 投資者中央媒體 | 令人費解的謎團 | | 令人費解的謎團 | |
令人費解的謎團 | | 令人費解的謎團 | >科學與時代媒介 | 現代印度教以上是用於高效大數據處理的強大 Python 生成器技術的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Tomergelistsinpython,YouCanusethe操作員,estextMethod,ListComprehension,Oritertools

在Python3中,可以通過多種方法連接兩個列表:1)使用 運算符,適用於小列表,但對大列表效率低;2)使用extend方法,適用於大列表,內存效率高,但會修改原列表;3)使用*運算符,適用於合併多個列表,不修改原列表;4)使用itertools.chain,適用於大數據集,內存效率高。

使用join()方法是Python中從列表連接字符串最有效的方法。 1)使用join()方法高效且易讀。 2)循環使用 運算符對大列表效率低。 3)列表推導式與join()結合適用於需要轉換的場景。 4)reduce()方法適用於其他類型歸約,但對字符串連接效率低。完整句子結束。

pythonexecutionistheprocessoftransformingpypythoncodeintoExecutablestructions.1)InternterPreterReadSthecode,ConvertingTingitIntObyTecode,whepythonvirtualmachine(pvm)theglobalinterpreterpreterpreterpreterlock(gil)the thepythonvirtualmachine(pvm)

Python的關鍵特性包括:1.語法簡潔易懂,適合初學者;2.動態類型系統,提高開發速度;3.豐富的標準庫,支持多種任務;4.強大的社區和生態系統,提供廣泛支持;5.解釋性,適合腳本和快速原型開發;6.多範式支持,適用於各種編程風格。

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用