Pythonリスト要素の抽出・置換・変換完全ガイド – 実用テクニック満載
Pythonでリストを操作する際、特定の要素を抽出したり、条件に基づいて置換・変換したりする処理は非常に頻繁に行われます。この記事では、リスト内の要素を効率的に抽出、置換、変換するための様々な手法を実例とともに詳しく解説します。初心者から上級者まで活用できる実用的なテクニックを網羅しています。
1. 特定の要素を抽出する基本的な方法
インデックスによる抽出
fruits = ["apple", "banana", "orange", "grape", "melon"]
first_fruit = fruits[0] # "apple"
last_fruit = fruits[-1] # "melon"
middle_fruits = fruits[1:4] # ["banana", "orange", "grape"]
条件による抽出(リスト内包表記)
numbers = [1, 5, 3, 8, 2, 7, 4, 6]
even_numbers = [x for x in numbers if x % 2 == 0]
print(even_numbers) # [8, 2, 4, 6]
large_numbers = [x for x in numbers if x > 5]
print(large_numbers) # [8, 7, 6]
2. filter()関数を使った抽出
def is_even(n):
return n % 2 == 0
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(is_even, numbers))
print(even_numbers) # [2, 4, 6, 8, 10]
# lambda関数を使用
positive_numbers = list(filter(lambda x: x > 0, [-2, -1, 0, 1, 2]))
print(positive_numbers) # [1, 2]
3. 文字列要素の抽出
特定の文字で始まる要素
words = ["apple", "banana", "apricot", "cherry", "avocado"]
a_words = [word for word in words if word.startswith("a")]
print(a_words) # ["apple", "apricot", "avocado"]
長さによる抽出
names = ["Tom", "Alice", "Bob", "Charlie", "Eve"]
short_names = [name for name in names if len(name) <= 3]
print(short_names) # ["Tom", "Bob", "Eve"]
正規表現による抽出
import re
emails = ["user@gmail.com", "invalid-email", "admin@company.co.jp", "test@yahoo.com"]
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
valid_emails = [email for email in emails if re.match(pattern, email)]
print(valid_emails) # ["user@gmail.com", "admin@company.co.jp", "test@yahoo.com"]
4. 要素の置換
特定の値を置換
numbers = [1, 2, 3, 2, 4, 2, 5]
# 2を20に置換
replaced = [20 if x == 2 else x for x in numbers]
print(replaced) # [1, 20, 3, 20, 4, 20, 5]
条件による置換
scores = [85, 45, 92, 38, 76, 88, 52]
# 60未満を"不合格"、以上を"合格"に置換
results = ["合格" if score >= 60 else "不合格" for score in scores]
print(results) # ["合格", "不合格", "合格", "不合格", "合格", "合格", "不合格"]
範囲による置換
temperatures = [-5, 0, 15, 25, 35, 42, 18]
categories = []
for temp in temperatures:
if temp < 0:
categories.append("氷点下")
elif temp < 20:
categories.append("涼しい")
elif temp < 30:
categories.append("暖かい")
else:
categories.append("暑い")
print(categories) # ["氷点下", "涼しい", "涼しい", "暖かい", "暑い", "暑い", "涼しい"]
5. map()関数を使った変換
基本的な変換
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))
print(squared) # [1, 4, 9, 16, 25]
# 文字列を大文字に変換
words = ["hello", "world", "python"]
uppercase = list(map(str.upper, words))
print(uppercase) # ["HELLO", "WORLD", "PYTHON"]
複数のリストを同時に処理
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
combined = list(map(lambda name, age: f"{name}({age}歳)", names, ages))
print(combined) # ["Alice(25歳)", "Bob(30歳)", "Charlie(35歳)"]
6. 辞書やオブジェクトからの抽出
辞書のリストから特定のキーを抽出
users = [
{"name": "Alice", "age": 25, "city": "Tokyo"},
{"name": "Bob", "age": 30, "city": "Osaka"},
{"name": "Charlie", "age": 35, "city": "Kyoto"}
]
names = [user["name"] for user in users]
print(names) # ["Alice", "Bob", "Charlie"]
tokyo_users = [user for user in users if user["city"] == "Tokyo"]
print(tokyo_users) # [{"name": "Alice", "age": 25, "city": "Tokyo"}]
オブジェクトの属性抽出
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
people = [Person("Alice", 25), Person("Bob", 30), Person("Charlie", 35)]
adult_names = [person.name for person in people if person.age >= 30]
print(adult_names) # ["Bob", "Charlie"]
7. 複雑な条件による抽出・変換
複数条件での抽出
products = [
{"name": "Laptop", "price": 80000, "category": "Electronics"},
{"name": "Book", "price": 1500, "category": "Education"},
{"name": "Phone", "price": 60000, "category": "Electronics"},
{"name": "Desk", "price": 25000, "category": "Furniture"}
]
# 電子機器で5万円以上の商品
expensive_electronics = [
product for product in products
if product["category"] == "Electronics" and product["price"] >= 50000
]
print([p["name"] for p in expensive_electronics]) # ["Laptop", "Phone"]
ネストした構造からの抽出
data = [
{"user": "Alice", "scores": [85, 92, 78]},
{"user": "Bob", "scores": [76, 88, 91]},
{"user": "Charlie", "scores": [95, 87, 89]}
]
# 各ユーザーの最高得点を抽出
max_scores = [{"user": item["user"], "max_score": max(item["scores"])} for item in data]
print(max_scores)
# [{"user": "Alice", "max_score": 92}, {"user": "Bob", "max_score": 91}, ...]
8. 数値データの変換
単位変換
celsius_temps = [0, 20, 30, 100]
fahrenheit_temps =
print(fahrenheit_temps) # [32.0, 68.0, 86.0, 212.0]
正規化(0-1の範囲に変換)
def normalize(data):
min_val, max_val = min(data), max(data)
range_val = max_val - min_val
return [(x - min_val) / range_val for x in data]
scores = [60, 75, 80, 95, 100]
normalized = normalize(scores)
print([round(x, 2) for x in normalized]) # [0.0, 0.38, 0.5, 0.88, 1.0]
9. 文字列の変換
大文字小文字の変換
names = ["alice", "BOB", "ChArLiE"]
capitalized = [name.capitalize() for name in names]
print(capitalized) # ["Alice", "Bob", "Charlie"]
文字列の整形
raw_data = [" apple ", "BANANA", " orange"]
cleaned = [item.strip().lower() for item in raw_data]
print(cleaned) # ["apple", "banana", "orange"]
文字列の分割と抽出
emails = ["user1@gmail.com", "admin@company.co.jp", "test@yahoo.com"]
domains = [email.split("@")[1] for email in emails]
print(domains) # ["gmail.com", "company.co.jp", "yahoo.com"]
10. 日付・時刻データの変換
from datetime import datetime
date_strings = ["2024-01-15", "2024-02-20", "2024-03-25"]
dates = [datetime.strptime(date_str, "%Y-%m-%d") for date_str in date_strings]
# 曜日を抽出
weekdays = [date.strftime("%A") for date in dates]
print(weekdays) # ["Monday", "Tuesday", "Monday"]
# 月名を抽出
months = [date.strftime("%B") for date in dates]
print(months) # ["January", "February", "March"]
11. エラーハンドリングを含む抽出・変換
安全な数値変換
def safe_int_convert(data):
result = []
for item in data:
try:
result.append(int(item))
except (ValueError, TypeError):
result.append(0) # デフォルト値
return result
mixed_data = ["123", "abc", "456", None, "789"]
converted = safe_int_convert(mixed_data)
print(converted) # [123, 0, 456, 0, 789]
辞書の安全なキーアクセス
data = [
{"name": "Alice", "age": 25},
{"name": "Bob"}, # ageキーがない
{"age": 30} # nameキーがない
]
names = [item.get("name", "Unknown") for item in data]
ages = [item.get("age", 0) for item in data]
print(names) # ["Alice", "Bob", "Unknown"]
print(ages) # [25, 0, 30]
12. パフォーマンスを考慮した処理
大きなデータセットの効率的な処理
def process_large_list(data, condition, transform):
"""大きなリストを効率的に処理"""
return [transform(item) for item in data if condition(item)]
# ジェネレータ式を使用(メモリ効率的)
def process_large_list_generator(data, condition, transform):
return (transform(item) for item in data if condition(item))
# 使用例
large_numbers = range(1000000)
result = process_large_list_generator(
large_numbers,
lambda x: x % 1000 == 0, # 1000で割り切れる数
lambda x: x ** 2 # 2乗する
)
print(list(result)[:5]) # [0, 1000000, 4000000, 9000000, 16000000]
13. 型変換とデータクリーニング
混在データの型統一
mixed_data = [1, "2", 3.0, "4.5", "invalid", None]
def clean_to_float(data):
result = []
for item in data:
try:
result.append(float(item))
except (ValueError, TypeError):
continue # 無効なデータは除外
return result
cleaned = clean_to_float(mixed_data)
print(cleaned) # [1.0, 2.0, 3.0, 4.5]
14. 階層データの平坦化
nested_list = [[1, 2], [3, 4, 5], [6], [7, 8, 9]]
# 1レベルの平坦化
flattened = [item for sublist in nested_list for item in sublist]
print(flattened) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
# より深いネストの平坦化
def flatten_deep(lst):
result = []
for item in lst:
if isinstance(item, list):
result.extend(flatten_deep(item))
else:
result.append(item)
return result
deep_nested = [1, [2, [3, 4]], 5, [6, 7]]
print(flatten_deep(deep_nested)) # [1, 2, 3, 4, 5, 6, 7]
15. 統計的変換
グループ化と集計
from collections import defaultdict
sales_data = [
{"product": "A", "amount": 100},
{"product": "B", "amount": 150},
{"product": "A", "amount": 200},
{"product": "C", "amount": 75}
]
# 商品別売上合計
product_totals = defaultdict(int)
for sale in sales_data:
product_totals[sale["product"]] += sale["amount"]
print(dict(product_totals)) # {"A": 300, "B": 150, "C": 75}
16. 実用的な応用例
CSVデータの処理
# CSVライクなデータの処理例
csv_data = [
"Alice,25,Engineer,75000",
"Bob,30,Designer,65000",
"Charlie,35,Manager,85000"
]
def parse_employee_data(data):
employees = []
for line in data:
name, age, position, salary = line.split(",")
employees.append({
"name": name,
"age": int(age),
"position": position,
"salary": int(salary)
})
return employees
employees = parse_employee_data(csv_data)
# 高給取りのエンジニアを抽出
high_paid_engineers = [
emp for emp in employees
if emp["position"] == "Engineer" and emp["salary"] > 70000
]
print(high_paid_engineers)
ログデータの解析
log_entries = [
"2024-01-15 10:30:25 [ERROR] Database connection failed",
"2024-01-15 10:30:26 [INFO] Retrying connection",
"2024-01-15 10:30:27 [ERROR] Authentication failed",
"2024-01-15 10:30:28 [INFO] Connection successful"
]
# エラーログのみ抽出
error_logs = [log for log in log_entries if "[ERROR]" in log]
# エラーメッセージのみ抽出
error_messages = [log.split("[ERROR]")[1].strip() for log in error_logs]
print(error_messages) # ["Database connection failed", "Authentication failed"]
17. 関数型プログラミングアプローチ
from functools import reduce
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 関数の組み合わせ
def compose_operations(data):
# 偶数のみ抽出 → 2乗 → 合計
evens = filter(lambda x: x % 2 == 0, data)
squared = map(lambda x: x ** 2, evens)
total = reduce(lambda x, y: x + y, squared)
return total
result = compose_operations(numbers)
print(result) # 220 (4 + 16 + 36 + 64 + 100)
まとめ
Pythonでリスト要素の抽出、置換、変換を行う方法は多岐にわたります。基本的なリスト内包表記から、filter()やmap()関数、さらには複雑な条件による処理まで、用途に応じて適切な手法を選択することが重要です。
特に大きなデータセットを扱う場合は、パフォーマンスとメモリ効率を考慮してジェネレータ式を使用したり、エラーハンドリングを適切に行うことで、堅牢で効率的なデータ処理プログラムを作成できます。これらのテクニックをマスターすることで、より高度なデータ分析や処理が可能になります。
■プロンプトだけでオリジナルアプリを開発・公開してみた!!
■AI時代の第一歩!「AI駆動開発コース」はじめました!
テックジム東京本校で先行開始。
■テックジム東京本校
「武田塾」のプログラミング版といえば「テックジム」。
講義動画なし、教科書なし。「進捗管理とコーチング」で効率学習。
より早く、より安く、しかも対面型のプログラミングスクールです。
<短期講習>5日で5万円の「Pythonミニキャンプ」開催中。
<月1開催>放送作家による映像ディレクター養成講座
<オンライン無料>ゼロから始めるPython爆速講座