はじめに:リスト

Python のデータ型

Jason Myers

Instructor

データ型

  • データ型システムは言語の機能の基盤となります
  • データ型を理解することで、データサイエンティストとしての能力が高まります
Python のデータ型

コンテナシーケンス

  • 他のデータ型を格納できます
  • 集計・並べ替えなどに使用されます
  • ミュータブル(listset)またはイミュータブル(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)
3
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...