リスト入門

Python のデータ型

Jason Myers

Instructor

データ型

  • データ型システムは、その言語が持つ機能の土台となります
  • データ型を理解することで、データサイエンティストとしての力が身につきます
Python のデータ型

コンテナシーケンス

  • 他のデータ型を格納します
  • 集計や並べ替えなどに使用します
  • list や set のようにミュータブル(変更可能)な場合と、tuple のようにイミュータブル(変更不可)な場合があります
  • イテラブル(反復可能)です
Python のデータ型

リスト

  • 追加した順序でデータを保持します
  • ミュータブル(変更可能)です
  • インデックスを持ちます
Python のデータ型

リスト内の単一要素へのアクセス

cookies = ['chocolate chip', 'peanut butter', 'sugar']
cookies.append('Tirggel')
print(cookies)
['chocolate chip', 'peanut butter', 'sugar', 'Tirggel']
print(cookies[2])
sugar
Python のデータ型

リストの結合

  • 演算子を使うと、2つのリストを結合して新しいリストを作成できます
cakes = ['strawberry', 'vanilla']

desserts = cookies + cakes

print(desserts)
['chocolate chip', 'peanut butter', 'sugar', 'Tirggel', 'strawberry', 'vanilla']
  • .extend() メソッドは、あるリストを別のリストの末尾に結合します
cookies.extend(cakes)
Python のデータ型

リスト内の要素を検索する

  • .index() メソッドは、リスト内のデータ要素の位置を特定します
position = cookies.index('sugar')

print(position)
2
Python のデータ型

リストから要素を削除する

  • .pop() メソッドは、リストから要素を削除し、その値を変数に保存できます
name = cookies.pop(position)

print(name)
sugar
print(cookies)
['chocolate chip', 'peanut butter', 'Tirggel']
Python のデータ型

リストを反復処理する

  • リスト内包表記 は、リストの各要素に処理を行うための一般的な反復方法です
titlecase_cookies = [cookie.title() for cookie in cookies]
print(titlecase_cookies)
Chocolate Chip
Peanut Butter
Tirggel
Python のデータ型

リストを並べ替える

  • sorted() 関数は、データを数値順またはアルファベット順に並べ替え、新しいリストを返します
print(cookies)
['chocolate chip', 'peanut butter', 'Tirggel']
sorted_cookies = sorted(cookies)

print(sorted_cookies)
['Tirggel', 'chocolate chip', 'peanut butter']
Python のデータ型

練習しましょう!

Python のデータ型

Preparing Video For Download...