目次
Step1 インプットデータ設定
1. 基本操作
1-1. 簡単定義方法
1次元リスト
sample_list_1d = [1,2,3,4,5]
>>> IN: print("sample_list_1d: ", sample_list_1d)
>>> OUT: sample_list_1d: [1, 2, 3, 4, 5]
2次元リスト
sample_list_2d = [[1,2,3,4,5],[6,7,8,9,10]]
>>> IN: print("sample_list_2d: ", sample_list_2d)
>>> OUT: sample_list_2d: [[1,2,3,4,5],[6,7,8,9,10]]
1-2. 要素アクセス [要素数:1]
1次元リスト
sample_list_1d = [1,2,3,4,5]
>>> IN[0]: print("sample_list_1d[0] = ",sample_list_1d[0])
>>> IN[1]: print("sample_list_1d[1] = ",sample_list_1d[1])
>>> IN[2]: print("sample_list_1d[2] = ",sample_list_1d[2])
>>> IN[3]: print("sample_list_1d[3] = ",sample_list_1d[3])
>>> IN[4]: print("sample_list_1d[4] = ",sample_list_1d[4])
>>> IN[5]: print("sample_list_1d[-1] = ",sample_list_1d[-1])
>>> OUT[0]: sample_list_1d[0] = 1
>>> OUT[1]: sample_list_1d[1] = 2
>>> OUT[2]: sample_list_1d[2] = 3
>>> OUT[3]: sample_list_1d[3] = 4
>>> OUT[4]: sample_list_1d[4] = 5
>>> OUT[5]: sample_list_1d[-1] = 5
2次元リスト
sample_list_2d = [[1,2,3,4,5],[6,7,8,9,10]]
>>> IN[0]: print("sample_list_2d[0][0] = ",sample_list_2d[0][0])
>>> IN[1]: print("sample_list_2d[0][1] = ",sample_list_2d[0][1])
>>> IN[2]: print("sample_list_2d[0][2] = ",sample_list_2d[0][2])
>>> IN[3]: print("sample_list_2d[0][3] = ",sample_list_2d[0][3])
>>> IN[4]: print("sample_list_2d[0][4] = ",sample_list_2d[0][4])
>>> IN[5]: print("sample_list_2d[1][0] = ",sample_list_2d[1][0])
>>> IN[6]: print("sample_list_2d[1][1] = ",sample_list_2d[1][1])
>>> IN[7]: print("sample_list_2d[1][2] = ",sample_list_2d[1][2])
>>> IN[8]: print("sample_list_2d[1][3] = ",sample_list_2d[1][3])
>>> IN[9]: print("sample_list_2d[1][4] = ",sample_list_2d[1][4])
>>> OUT[0]: sample_list_1d[0] = 1
>>> OUT[1]: sample_list_1d[1] = 2
>>> OUT[2]: sample_list_1d[2] = 3
>>> OUT[3]: sample_list_1d[3] = 4
>>> OUT[4]: sample_list_1d[4] = 5
>>> OUT[5]: sample_list_1d[4] = 6
>>> OUT[6]: sample_list_1d[4] = 7
>>> OUT[7]: sample_list_1d[4] = 8
>>> OUT[8]: sample_list_1d[4] = 9
>>> OUT[9]: sample_list_1d[4] = 10
1-3. 要素アクセス [要素数:複数]
1次元リスト
sample_list_1d = [1,2,3,4,5]
start_id = 1
end_id = 3
>>> print("項目番号「1」から「3」:",sample_list_1d[start_id:end_id+1])
>>> print("項目番号「最初」から「3」:",sample_list_1d[:end_id+1])
>>> print("項目番号「1」から「最後」:",sample_list_1d[start_id:])
>>> print("全要素:",sample_list_1d[:])
>>> 項目番号「1」から「3」: [2, 3, 4]
>>> 項目番号「最初」から「3」: [1, 2, 3, 4]
>>> 項目番号「1」から「最後」: [2, 3, 4, 5]
>>> 全要素: [1, 2, 3, 4, 5]
1-4. リスト長さ (List Length)
1次元リスト
sample_list_1d = [1,2,3,4,5]
print("リスト長さ:",len(sample_list_1d))
リスト長さ: 5
2次元リスト
sample_list_2d = [[1,2,3,4,5],[6,7,8,9,10]]
print("親リスト長さ:",len(sample_list_2d)) ### 親リスト
print("第一子リスト長さ:",len(sample_list_2d[0])) ### 第一子リスト
親リスト長さ: 2
第一子リスト長さ: 5
リスト長さ: 5
1-5. 格納型 (Data Type)
1次元リスト
sample_list = [1,2,3,4,5]
print("格納型:",type(sample_list))
格納型: <class 'list'>
1-6. 要素データ型 (Element Data Type)
1次元リスト
sample_list = [1,2,3,4,5]
print("要素データ型:",type(sample_list[0]))
要素データ型: <class 'int'>
1-7. 要素追加 (Element Append)
1次元リスト
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 3, 4, 5]
sample_list.append(6)
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 3, 4, 5, 6]
1-8. 指定位置要素追加 [要素数:1]
1次元リスト
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 3, 4, 5]
specified_id = 2
specified_value = 8
sample_list.insert(specified_id,specified_value)
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 8, 3, 4, 5]
1-9. 指定位置要素追加 [要素数:複数]
1次元リスト
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 3, 4, 5]
specified_id = 2
specified_value_list = [8,8,8]
sample_list[specified_id:specified_id] = specified_value_list
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 8, 8, 8, 3, 4, 5]
1-10. 要素削除 [値]
1次元リスト
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 3, 4, 5]
sample_list.remove(5)
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 3, 4]
1次元リスト[複数ある場合]
sample_list = [1,2,3,1,2,3]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 3, 1, 2, 3]
sample_list.remove(1)
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [2, 3, 1, 2, 3]
1-11. 要素削除 [項目番号] [手法1]
1次元リスト
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 3, 4, 5]
del sample_list[2]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 4, 5]
1次元リスト[複数]
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 3, 4, 5]
del sample_list[1:4]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 5]
1-12. 要素削除 [項目番号] [手法2]
1次元リスト
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 3, 4, 5]
specified_id = 2
sample_list[specified_id:specified_id+1] = []
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 4, 5]
1次元リスト[複数]
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 3, 4, 5]
specified_start_id = 1
specified_end_id = 3
sample_list[specified_start_id:specified_end_id+1] = []
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 5]
1-13. 全要素削除
1次元リスト
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: [1, 2, 3, 4, 5]
sample_list.clear()
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: []
1-14. リスト結合 (手法1)
1次元リスト
sample_list = ["H","e","l","l","o"]
extend_list = ["W","o","r","l","d"]
sample_list.extend(extend_list)
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
1-15. リスト結合 (手法2)
1次元リスト
sample_list = ["H","e","l","l","o"]
extend_list = ["W","o","r","l","d"]
sample_list = sample_list + extend_list
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list: ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
2. データ定義
2-1. 零リスト (Zero List)
1次元リスト
array_length = 10
zero_list = [0] * array_length
>>> IN: print("zero list: ",zero_list)
>>> OUT: zero list: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
2-2. 特定値リスト (Specified Value List)
1次元リスト
specified_value = 3
array_length = 10
specified_value_list = [specified_value] * array_length
>>> IN: print("specified value list: ",specified_value_list)
>>> OUT: specified value list: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
2-3. 空白リスト (Empty List)
1次元リスト[要素数:1]
empty_list = []
print("empty list: ",empty_list)
empty list: []
1次元リスト[要素数:複数]
array_length = 10
empty_list = [[]] * array_length
print("empty list: ",empty_list)
empty list: [[], [], [], [], [], [], [], [], [], []]
2-4. 等差数列リスト (Arithmetic Sequence List)
1次元リスト
start_num = 1
end_num = 10
difference = 1
arithmetic_sequence_list = list(range(start_num, end_num + 1, difference))
>>> IN: print("arithmetic sequence list: ",arithmetic_sequence_list)
>>> OUT: arithmetic sequence list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3. 基礎変換
3-1. 要素データ型変更
1次元リスト
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
### sample list: [1, 2, 3, 4, 5]
sample_list = list(map(str,sample_list))
>>> IN: print("sample list: ",sample_list)
### sample list: ['1', '2', '3', '4', '5']
3-2. 「string list」to「string」
1次元リスト
sample_list= ["H","e","l","l","o"]
>>> IN: print("sample list: ",sample_list)
### OUT: sample list: ['H', 'e', 'l', 'l', 'o']
joint_value = ""
sample_string = joint_value.join(sample_list)
>>> IN: print("sample string: ",sample_string)
### OUT: sample string: Hello
3-3. 「string」to「string list」
1次元リスト
sample_string = "HelloWorld"
>>> IN: print("sample string: ",sample_string)
### sample string: HelloWorld
sample_list= list(sample_string)
>>> IN: print("sample string: ",sample_string)
### OUT: sample list: ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
3-4. 「sentence」to「word list」
1次元リスト
sample_sentence = "Hello World"
>>> IN: print("sample sentence: ",sample_sentence)
### OUT: sample sentence: Hello World
split_value = " "
sample_list= sample_sentence.split(split_value)
>>> IN: print("sample list: ",sample_list)
### OUT: sample list: ['Hello', 'World']
4. 格納形式変換
4-1. numpy ndarray
4-1-1. 「list」to「numpy ndarray」
1次元リスト
import numpy as np
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> IN: print("type: ",type(sample_list))sample
### OUT: list: [1, 2, 3, 4, 5]
### OUT: type: <class 'list'>
sample_ndarray = np.array(sample_list)
>>> IN: print("sample ndarray: ",sample_ndarray)
>>> IN: print("type: ",type(sample_ndarray))
### OUT: sample ndarray: [1 2 3 4 5]
### OUT: type: <class 'numpy.ndarray'>
4-1-2. 「numpy ndarray」to「list」
1次元リスト
import numpy as np
sample_ndarray = np.array([1,2,3,4,5])
>>> IN: print("sample ndarray: ",sample_ndarray)
>>> IN: print("type: ",type(sample_ndarray))
### OUT: sample ndarray: [1 2 3 4 5]
### OUT: type: <class 'numpy.ndarray'>
sample_list = sample_ndarray.tolist()
>>> IN: print("sample list: ",sample_list)
>>> IN: print("type: ",type(sample_list))
### OUT: sample list: [1, 2, 3, 4, 5]
### OUT: type: <class 'list'>
4-2. numpy matrix
4-2-1. 「list」to「numpy matrix」
1次元リスト
import numpy as np
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> IN: print("type: ",type(sample_list))
### OUT: sample list: [1, 2, 3, 4, 5]
### OUT: type: <class 'list'>
sample_matrix = np.matrix(sample_list)
>>> IN: print("sample matrix: ",sample_matrix)
>>> IN: print("type: ",type(sample_matrix))
### OUT: sample matrix: [[1 2 3 4 5]]
### OUT: type: <class 'numpy.matrix'>
4-2-2. 「numpy matrix」to「list」
1次元リスト
import numpy as np
sample_matrix = np.matrix([1,2,3,4,5])
>>> IN: print("sample matrix: ",sample_matrix)
>>> IN: print("type: ",type(sample_matrix))
### OUT: sample matrix: [[1 2 3 4 5]]
### OUT: type: <class 'numpy.matrix'>
sample_list = sample_matrix.tolist()
>>> IN: print("sample list: ",sample_list)
>>> IN: print("type: ",type(sample_list))
### OUT: sample list: [[1, 2, 3, 4, 5]]
### OUT: type: <class 'list'>
5. ファイル入力
5-1. テキストファイル( .txt )
5-2. 表形式ファイル( .csv )
5-3. 画像ファイル( .png )
5-4. オブジェクトファイル( .obj )
Step2 データ操作 / アルゴリズム
1. 探索
1-1. 最大値
1次元リスト
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
print("Maximum Value: ",max(sample_list))
sample list: [1, 2, 3, 4, 5]
Maximum Value: 5
1-2. 最大値要素番号
1次元リスト
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list: [1, 2, 3, 4, 5]
max_value_id = [i for i, v in enumerate(sample_list) if v == max(sample_list)]
print("Maximum Value ID: ",max_value_id)
Maximum Value ID: [4]
1次元リスト[複数ある場合]
sample_list = [1,2,3,1,2,3]
print("sample list: ",sample_list)
sample list: [1, 2, 3, 1, 2, 3]
max_value_id = [i for i, v in enumerate(sample_list) if v == max(sample_list)]
print("Maximum Value ID: ",max_value_id)
Maximum Value ID: [2, 5]
1-3. 最小値
1次元リスト
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
print("Minimum Value: ",min(sample_list))
sample list: [1, 2, 3, 4, 5]
Minimum Value: 1
1-4. 最小値要素番号
1次元リスト
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list: [1, 2, 3, 4, 5]
min_value_id = [i for i, v in enumerate(sample_list) if v == min(sample_list)]
print("Minimum Value ID: ",min_value_id)
Minimum Value ID: [0]
1次元リスト[複数ある場合]
sample_list = [1,2,3,1,2,3]
print("sample list: ",sample_list)
sample list: [1, 2, 3, 1, 2, 3]
min_value_id = [i for i, v in enumerate(sample_list) if v == min(sample_list)]
print("Minimum Value ID: ",min_value_id)
Minimum Value ID: [0, 3]
1-5. トップ値[大きい順]
1次元リスト
import heapq
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list: [1, 2, 3, 4, 5]
print("Top Large Value: ",heapq.nlargest(3, sample_list))
Top Large Value: [5, 4, 3]
1-6. トップ値[小さい順]
1次元リスト
import heapq
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list: [1, 2, 3, 4, 5]
print("Top Small Value: ",heapq.nsmallest(3, sample_list))
Top Small Value: [1, 2, 3]
1-7. 特定値有無
1次元リスト
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list: [1, 2, 3, 4, 5]
print("Have value '1': ",1 in sample_list)
print("Have value '6': ",6 in sample_list)
Have value '1': True
Have value '6': False
1-8. 特定値要素番号
1次元リスト
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list: [1, 2, 3, 4, 5]
print("Search ID for value '1': ",sample_list.index(1))
Search ID for value '1': 0
1-9. 特定値カウント
1次元リスト
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list: [1, 2, 3, 3, 3, 3, 3, 4, 5]
print("Count value '3': ",sample_list.count(3))
Count value '3': 5
2. 順番変更 / ソート
2-1. 逆順
1次元リスト
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample_list.reverse()
print("reversed list: ",sample_list)
2-2. ソート [昇順]
1次元リスト
sample_list = [5,4,3,2,1]
print("sample list: ",sample_list)
sorted_list = sorted(sample_list)
print("sorted list: ",sorted_list)
2-3. ソート [降順]
1次元リスト
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sorted_list = sorted(sample_list,reverse=True)
print("sorted list: ",sorted_list)
2-4. ソート [昇順] [絶対値]
1次元リスト
sample_list = [5,-4,3,-2,1]
print("sample list: ",sample_list)
sorted_list = sorted(sample_list,key=abs)
print("sorted list [abs]: ",sorted_list)
2-5. ソート [昇順] [文字列長さ]
1次元リスト
sample_list = ["zero","one","two","three","four","five"]
print("sample list: ",sample_list)
sorted_list = sorted(sample_list,key=len)
print("sorted list [string length]: ",sorted_list)
3. リスト計算
3-1. 足し算
1次元リスト
A = [4,5,6]
B = [1,2,3]
C = [x + y for (x, y) in zip(A, B)]
print("A: ",A)
print("B: ",B)
print("add operation: ",C)
3-2. 引き算
1次元リスト
A = [4,5,6]
B = [1,2,3]
C = [x - y for (x, y) in zip(A, B)]
print("A: ",A)
print("B: ",B)
print("substract operation: ",C)
3-3. かけ算
1次元リスト
A = [4,5,6]
B = [1,2,3]
C = [x - y for (x, y) in zip(A, B)]
print("A: ",A)
print("B: ",B)
print("multiply operation: ",C)
3-4. わり算
1次元リスト
A = [4,5,6]
B = [1,2,3]
C = [x / y for (x, y) in zip(A, B)]
print("A: ",A)
print("B: ",B)
print("divide operation: ",C)
3-5. 余り
1次元リスト
A = [10,10,10]
B = [1,2,3]
C = [x % y for (x, y) in zip(A, B)]
print("A: ",A)
print("B: ",B)
print("mod operation: ",C)
3-6. 総和
1次元リスト
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sum_value = sum(sample_list)
print("Sum Value: ",sum_value)