0Pricing
Python For Kids · レッスン

データ構造の操作

並べ替え、スライス、反復処理を練習します。

「データ構造の操作」はCoddyKit上の無料Python For Kidsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはPython For Kids学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Python For Kidsコースには全4レッスンが含まれています。

データ操作の概要

データ構造を操作する

データ構造のレッスンへようこそ!今日は、リストやタプルを並べ替えたり、スライスしたり、反復処理したりする方法を学びます。

データ構造の操作 — イラスト1

データ操作とは

データ操作とは、データ構造に保存されたデータを変更、整理、分析することです。プログラムでデータを効率的に管理し、活用するのに役立ちます。

  • ソート:データを特定の順序に並べます。
  • スライス:データの一部を取り出します。
  • 反復処理:データ構造内の各要素を順番に処理します。

リストをソートする

ソートすると、リストの要素を昇順や降順など、特定の順序に並べ替えられます。Pythonには、リストを簡単にソートするための組み込みメソッドが用意されています。

例:

# Creates a list of numbers
numbers = [4, 2, 9, 1, 5]

# Sorts the list in ascending order
numbers.sort()

# Prints the sorted list
print(numbers)  # Output: [1, 2, 4, 5, 9]

リストとタプルをスライスする

スライスを使うと、リストやタプルから要素の一部を取り出せます。開始インデックスと終了インデックスを指定して、データの一部を抽出できます。

例:

# Creates a list of fruits
fruits = ["apple", "banana", "cherry", "date", "elderberry"]

# Slices the list from index 1 to 3 (excluding 4)
subset = fruits[1:4]

# Prints the sliced list
print(subset)  # Output: ['banana', 'cherry', 'date']

データ構造を反復処理する

反復処理とは、データ構造内の各要素を1つずつ順番に処理することです。forループやwhileループなどのループを使って、リストやタプルを反復処理します。

例:

# Creates a list of colors
colors = ["red", "green", "blue"]

# Iterates through the list and prints each color
for color in colors:
    print(color)

リスト内包表記

リスト内包表記を使うと、リストを簡潔に作成できます。コードを短くし、読みやすくするのに役立ちます。

例:

# Creates a list of squares from 1 to 5 using list comprehension
squares = [x**2 for x in range(1, 6)]

# Prints the list of squares
print(squares)  # Output: [1, 4, 9, 16, 25]

キーを使ってソートする

sort()メソッドのkeyパラメータを使うと、特定の基準に基づいてリストをソートできます。

例:

# Creates a list of tuples with names and ages
people = [("Alice", 30), ("Bob", 25), ("Charlie", 35)]

# Sorts the list by age using a key
people.sort(key=lambda person: person[1])

# Prints the sorted list
print(people)  # Output: [('Bob', 25), ('Alice', 30), ('Charlie', 35)]

サンプルプログラム:学生の成績

成績順に学生を並べ替え、上位の学生をスライスで取り出すプログラムを作成しましょう。

コード:

# Creates a list of student grades as tuples
student_grades = [
    ("Alice", 88),
    ("Bob", 92),
    ("Charlie", 78),
    ("Diana", 85),
    ("Ethan", 95)
]

# Sorts the list by grade in descending order
student_grades.sort(key=lambda student: student[1], reverse=True)

# Slices the top 3 students
top_students = student_grades[:3]

# Prints the top students
print("Top Students:")
for student in top_students:
    print(student[0] + " scored " + str(student[1]))
    
# Output:
# Top Students:
# Ethan scored 95
# Bob scored 92
# Alice scored 88
# Creates a list of numbers
numbers = [10, 5, 8, 3, 7]

# Sorts the list in ascending order
numbers.sort()

# Slices the first three numbers
top_three = numbers[:3]

# Prints the sliced list
print(top_three)

よくできました!

Pythonでリストやタプルをソート、スライス、反復処理して、データ構造を操作する方法を学びました。これらのスキルは、プログラムでデータを効果的に整理・管理するのに役立ちます。さまざまなデータ構造とその操作方法を試しながら、練習を続けてください。

データ構造の操作 — イラスト10

よくある質問

「データ構造の操作」レッスンは無料ですか?

はい。「データ構造の操作」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Python For Kidsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Python For Kidsコースには全4レッスンが含まれています。

「データ構造の操作」で何を学びますか?

並べ替え、スライス、反復処理を練習します。 ブラウザで直接実行するハンズオンコードでPython For Kidsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Python For Kidsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのPython For Kidsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「データ構造の操作」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このPython For Kidsレッスンでコードを書いて実行できますか?

はい。すべてのPython For Kidsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. リストとタプル
  2. 辞書
  3. 集合
  4. データ構造の操作
← Python For Kidsに戻る