LoginSignup
0
0

Pythonの配列の宣言をまとめてみた

Last updated at Posted at 2024-01-31

Pythonの配列についてまとめました。

Pythonの配列について

Pythonには4種類の配列があります。

ここでは、list型、tuple型、dictionary型、set型について記述します。

list型

A = list()
A = []
A = [1]

list型は、このどちらかで宣言することができる。

tuple型

B = tuple()
B = ()
B = (1,)  # 1要素の場合カンマが必要、カンマがないと優先演算の括弧に解釈される
B = 1,    # カンマがあれば括弧は省略可能、省略できない場合もある
B = (1, 2, 3)

tuple型は、基本的に要素を変更できない。

dictionary型

C = dict()
C = {}
C = {"key": "value"}
C = {"one": 1, 1: "one"}

dictionary型は任意の文字列や数字などを添え字のように指定できる。

set型

D = set()  # 空集合は set() でしか作れない
D = {1}
D = {1, 2, 3}

set型は配列内の要素が常に一意になる。

0
0
1

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