0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WindowsによるPython入門 #06: リスト

Last updated at Posted at 2023-02-24

はじめに

今回はデータ構造の型であるタプル型とリスト型を解説します。

YouTube動画

タプル型

タプル型は、複数のオブジェクトの配列を表すデータ型で、以下の特徴を持ちます。

  • 不変
  • シーケンス
  • イテラブル

配列データなので、データを順番に並べたものです。
リテラルとして、() や「カンマ」で生成出来ます。
また、コンストラクタを使用しても作成出来ます。

>>> ()  # 空のタプル
()
>>> type(_)
<class 'tuple'>
>>> (1, 2, 3)
(1, 2, 3)
>>> type(_)
<class 'tuple'>
>>> 1, 2, 3  # ()を付けなくても良い
(1, 2, 3)
>>> type(_)
<class 'tuple'>
>>> 1,  # ()を付けなくてもタプル
(1,)
>>> type(_)
<class 'tuple'>
>>> (1,)  # ()を付けても良い
(1,)
>>> type(_)
<class 'tuple'>
>>> (1)  # ()を付けてもカンマがなければタプルではない
1
>>> type(_)
<class 'int'>
>>> len(1,)  # 関数の引数に渡す場合は、()が必要
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()
>>> len((1,))
1
>>> tuple("abc")  # 文字列から作成
('a', 'b', 'c')
>>> tuple([1, 2, 3])  # リストから作成
(1, 2, 3)
>>> (1, "abc", 3.0)  # 複数の型のオブジェクトが混在してもOK
(1, 'abc', 3.0)

アンパック代入

一度に複数の変数にオブジェクトを代入出来ます。

>>> a, b = (1, 2)
>>> a
1
>>> b
2
>>> a, b, c = (1, 2)  # 個数が一致しないとエラー
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 2)
>>> a, b = (1, 2, 3)  # 個数が一致しないとエラー
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
>>> a, *b = (1, 2, 3)  # bに残りをまとめて代入
>>> a
1
>>> b
[2, 3]

リスト型

リスト型は、タプル型同様に複数のオブジェクトの配列を表すデータ型で、以下の特徴を持ちます。

  • 可変
  • シーケンス
  • イテラブル

リテラルとして、[] で生成出来ます。
また、コンストラクタを使用しても作成出来ます。

>>> []  # 空のリスト
[]
>>> type(_)
<class 'list'>
>>> [1, 2, 3]
[1, 2, 3]
>>> type(_)
<class 'list'>
>>> [1, "abc", 3.0]  # 同じ型でなくても良い
[1, 'abc', 3.0]
>>> type(_)
<class 'list'>
>>> list()  # 空のリスト
[]
>>> list((1, 2, 3))  # タプルはイテラブルなのでコンストラクタで指定可能
[1, 2, 3]
>>> list("abc")  # 文字列もイテラブル
['a', 'b', 'c']
>>> list(range(3))  # rangeもイテラブル
[0, 1, 2]

可変なので、内部データを変更可能です。

>>> a = [1, 2, 3]
>>> a[0] = 4  # 最初の要素を変更
>>> a
[4, 2, 3]
>>> a.append(5)  # 末尾に要素を追加
>>> a
[4, 2, 3, 5]
>>> a[1:3] = [6, 7, 8]  # インデックス1から2までの範囲を置き換え
>>> a
[4, 6, 7, 8, 5]
>>> a.insert(1, 9)  # インデックス1に値9を挿入
>>> a
[4, 9, 6, 7, 8, 5]
>>> a = [1, 2, 3]
>>> a.extend([4, 5])  # リストを追加
>>> a
[1, 2, 3, 4, 5]
>>> a += [6, 7]  # リストを追加
>>> a
[1, 2, 3, 4, 5, 6, 7]
>>> del a[0]  # 要素の削除
>>> a
[2, 3, 4, 5, 6, 7]
>>> del a[1:3]  # 要素の削除
>>> a
[2, 5, 6, 7]
>>> 2 in [1, 2, 3]  # 要素の確認
True
>>> 9 in [1, 2, 3]
False

リスト型のメソッドに関して、sort() のみ解説します。
sort() はリストの内容を変更します。
sorted() はリストの内容を変更せず、ソート済みの新たなリストを返します。

>>> a = [2, 5, 6, 1]
>>> a.sort()  # リストの内容を変更
>>> a
[1, 2, 5, 6]
>>> a = [2, 5, 6, 1]
>>> sorted(a)  # リストの内容を変更せず、新たなリストを返す
[1, 2, 5, 6]
>>> a
[2, 5, 6, 1]

リスト内包表記

リスト内容表記を使用してもリストを作成出来ます。次のような構文です。
[式 for 変数 in イテラブル if 条件]

>>> [num + 1 for num in range(5)]
[1, 2, 3, 4, 5]
>>> [num for num in range(10) if num % 2 == 0]  # 条件式も使用可能
[0, 2, 4, 6, 8]

インデクシングとスライス

タプルとリストのインデクシングとスライスです。

>>> (1, 2, 3)[0]  # インデクシング
1
>>> (1, 2, 3)[1:]  # スライス
(2, 3)
>>> a = [1, 2, 3]
>>> a[0]  # インデクシング
1
>>> a[1:]  # スライス
[2, 3]

タプルとリストの演算

タプル型やリスト型は、足し算、掛け算、比較などの演算が出来ます。

>>> (1, 2) + (3, 4)
(1, 2, 3, 4)
>>> (1, 2) * 2
(1, 2, 1, 2)
>>> (1, 2) < (1, 3)  # 最初の要素が 1 == 1 なので、2番目の 2 < 3を比較
True
>>> (1, 2) < (1, 1, 1)  # 2番目の 2 < 1 を比較
False
>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
>>> [1, 2] * 2
[1, 2, 1, 2]
>>> [1, 2, 3] < [1, 2, 2]
False
>>> [1, 2, 3] == [1, 2, 3]
True
>>> (1, 2, 3) == [1, 2, 3]
False
>>> (1, 2, 3) < [1, 2, 2]  # タプルとリストの大小比較はエラー
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'tuple' and 'list'

おわりに

以上、タプル型とリスト型に関して解説しました。
次回は、辞書型と集合型に関して解説します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?