1D list
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]
2D list
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]]
1D list
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
2D list
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
1D list
sample_list_1d = [1,2,3,4,5]
start_id = 1
end_id = 3
>>> print("Item numbers "1" to "3":",sample_list_1d[start_id:end_id+1])
>>> print("Item number "First" to "3":",sample_list_1d[:end_id+1])
>>> print("Item number "1" to "last":",sample_list_1d[start_id:])
>>> print("All elements:",sample_list_1d[:])
>>>Item numbers "1" to "3":[2, 3, 4]
>>>Item number "First" to "3":[1, 2, 3, 4]
>>>Item number "1" to "last":[2, 3, 4, 5]
>>>All elements:[1, 2, 3, 4, 5]
1D list
sample_list_1d = [1,2,3,4,5]
print("List length:",len(sample_list_1d))
List length: 5
2D list
sample_list_2d = [[1,2,3,4,5],[6,7,8,9,10]]
print("Parent list length:",len(sample_list_2d)) ###Parent list
print("First child list length:",len(sample_list_2d[0])) ###First child list
Parent list length: 2
First child list length: 5
List length: 5
1D list
sample_list = [1,2,3,4,5]
print("Storage type:",type(sample_list))
Storage type:<class 'list'>
1D list
sample_list = [1,2,3,4,5]
print("Element data type:",type(sample_list[0]))
Element data type:<class 'int'>
1D list
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]
1D list
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]
1D list
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]
1D list
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]
1D list[If there are multiple]
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]
1D list
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]
1D list[Multiple]
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]
1D list
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]
1D list[Multiple]
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]
1D list
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: []
1D list
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']
1D list
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']
1D list
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]
1D list
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]
1D list[Number of elements: 1]
empty_list = []
print("empty list: ",empty_list)
empty list: []
1D list[Number of elements: Multiple]
array_length = 10
empty_list = [[]] * array_length
print("empty list: ",empty_list)
empty list: [[], [], [], [], [], [], [], [], [], []]
1D list
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]
1D list
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」
1D list
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」
1D list
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」
1D list
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-1. numpy ndarray
4-1-1. 「list」to「numpy ndarray」
1D list
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」
1D list
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」
1D list
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」
1D list
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'>
1D list
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
1D list
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]
1D list[If there are multiple]
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]
1D list
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
1D list
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]
1D list[If there are multiple]
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]
1D list
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]
1D list
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]
1D list
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
1D list
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
1D list
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
1D list
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample_list.reverse()
print("reversed list: ",sample_list)
1D list
sample_list = [5,4,3,2,1]
print("sample list: ",sample_list)
sorted_list = sorted(sample_list)
print("sorted list: ",sorted_list)
1D list
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sorted_list = sorted(sample_list,reverse=True)
print("sorted list: ",sorted_list)
1D list
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)
1D list
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)
1D list
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)
1D list
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)
1D list
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)
1D list
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)
1D list
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)
1D list
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sum_value = sum(sample_list)
print("Sum Value: ",sum_value)
Recommended Posts