0
1

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.

Pythonのリスト操作と初期化

Last updated at Posted at 2023-10-29

はじめに

リストの操作と初期化をまとめる。

リスト操作

操作 コマンド
パラメータ追加 list.append( 追加したいパラメータ )
パラメータ削除 list.remove( 削除したいパラメータ )
パラメータ抜き出し
varに抜き出したパラメータの値が入る。
var = list.pop( 抜き出したいパラメータ位置 )
リスト初期化 list.clear()
空リストになる
リスト結合 list.extend( list2 )

リスト操作と初期化の例

list_test.py
#----------------------------------
#空リスト
#----------------------------------
list_test=[]

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 0 []

#----------------------------------
#リストにパラメータ追加
#----------------------------------
for i in range(10):
	list_test.append(i*10)

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 10 [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

#----------------------------------
#リスト内のパラメータ削除
#----------------------------------
list_test.remove(60)	# 60 を削除

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 9 [0, 10, 20, 30, 40, 50, 70, 80, 90]

#----------------------------------
#リスト内のパラメータ抜き出し
#----------------------------------
print(list_test.pop(5))	# 5番目のデータを抜き出し

print('# type:',type(list_test),'length:',len(list_test),list_test)
# 50
# type: <class 'list'> length: 8 [0, 10, 20, 30, 40, 70, 80, 90]

print(list_test.pop())	# 最後データを抜き出し

print('# type:',type(list_test),'length:',len(list_test),list_test)
# 90
# type: <class 'list'> length: 7 [0, 10, 20, 30, 40, 70, 80]

#----------------------------------
#リスト内のパラメータ全削除
#----------------------------------
list_test.clear()

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 0 []

#----------------------------------
#リストを特定の値で初期化(1次元)
#----------------------------------
list_test=[0]*5

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 5 [0, 0, 0, 0, 0]

#----------------------------------
#リストを特定の値で初期化(2次元)
#----------------------------------
list_test=[[1]*5]*3

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 3 [[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]

#----------------------------------
#リスト結合
#----------------------------------
list_test=[0]*5
list_test2=[1]*5
list_test.extend(list_test2)

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 10 [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]

#----------------------------------
#内包表記で初期化
#----------------------------------
# 1次元----------------------------
list_test =  [i*10 for i in range(10)]

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 10 [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

# 1次元(既存リストを0初期化)-------
list_test =  [0 for i in list_test]

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 10 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

# 2次元----------------------------
list_test = [[j for j in range(5)] for i in range(3)]

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 3 [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]

# 2次元(既存リストを0初期化)-------
list_test = [[0 for j in i] for i in list_test]

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 3 [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

#----------------------------------
# ASCII文字列で初期化
#----------------------------------
# chr()で数値→ASCII変換する。 ord()は該当のASCIIの数値を求めるために使っているが、chr(ord('a')+i) は chr(0x61+i) でもよい。
# 小文字アルファベット----------------------------
list_test =  [chr(ord('a')+i) for i in range(26)]

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 26 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

# 大文字アルファベット----------------------------
list_test =  [chr(ord('A')+i) for i in range(26)]

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 26 ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

# 数値-------------------------------------------
list_test =  [chr(ord('0')+i) for i in range(10)]

print('# type:',type(list_test),'length:',len(list_test),list_test)
# type: <class 'list'> length: 10 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

以上

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?