Liste unidimensionnelle
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]
Liste 2D
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]]
Liste unidimensionnelle
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
Liste 2D
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
Liste unidimensionnelle
sample_list_1d = [1,2,3,4,5]
start_id = 1
end_id = 3
>>> print("Numéros d'article "1" à "3":",sample_list_1d[start_id:end_id+1])
>>> print("Numéro d'article "Premier" à "3":",sample_list_1d[:end_id+1])
>>> print("Numéro d'article "1" à "dernier":",sample_list_1d[start_id:])
>>> print("Tous les éléments:",sample_list_1d[:])
>>>Numéros d'article "1" à "3":[2, 3, 4]
>>>Numéro d'article "Premier" à "3":[1, 2, 3, 4]
>>>Numéro d'article "1" à "dernier":[2, 3, 4, 5]
>>>Tous les éléments:[1, 2, 3, 4, 5]
Liste unidimensionnelle
sample_list_1d = [1,2,3,4,5]
print("Longueur de la liste:",len(sample_list_1d))
Longueur de la liste: 5
Liste 2D
sample_list_2d = [[1,2,3,4,5],[6,7,8,9,10]]
print("Longueur de la liste des parents:",len(sample_list_2d)) ###Liste des parents
print("Longueur de la première liste enfant:",len(sample_list_2d[0])) ###Première liste d'enfants
Longueur de la liste des parents: 2
Longueur de la première liste enfant: 5
Longueur de la liste: 5
Liste unidimensionnelle
sample_list = [1,2,3,4,5]
print("Type de stockage:",type(sample_list))
Type de stockage:<class 'list'>
Liste unidimensionnelle
sample_list = [1,2,3,4,5]
print("Type de données d'élément:",type(sample_list[0]))
Type de données d'élément:<class 'int'>
Liste unidimensionnelle
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]
Liste unidimensionnelle
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]
Liste unidimensionnelle
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]
Liste unidimensionnelle
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]
Liste unidimensionnelle[S'il y a plusieurs]
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]
Liste unidimensionnelle
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]
Liste unidimensionnelle[Plusieurs]
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]
Liste unidimensionnelle
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]
Liste unidimensionnelle[Plusieurs]
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]
Liste unidimensionnelle
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: []
Liste unidimensionnelle
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']
Liste unidimensionnelle
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']
Liste unidimensionnelle
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]
Liste unidimensionnelle
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]
Liste unidimensionnelle[Nombre d'éléments: 1]
empty_list = []
print("empty list: ",empty_list)
empty list: []
Liste unidimensionnelle[Nombre d'éléments: multiple]
array_length = 10
empty_list = [[]] * array_length
print("empty list: ",empty_list)
empty list: [[], [], [], [], [], [], [], [], [], []]
Liste unidimensionnelle
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]
Liste unidimensionnelle
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」
Liste unidimensionnelle
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」
Liste unidimensionnelle
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」
Liste unidimensionnelle
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」
Liste unidimensionnelle
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」
Liste unidimensionnelle
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」
Liste unidimensionnelle
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」
Liste unidimensionnelle
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'>
Liste unidimensionnelle
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
Liste unidimensionnelle
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]
Liste unidimensionnelle[S'il y a plusieurs]
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]
Liste unidimensionnelle
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
Liste unidimensionnelle
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]
Liste unidimensionnelle[S'il y a plusieurs]
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]
Liste unidimensionnelle
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]
Liste unidimensionnelle
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]
Liste unidimensionnelle
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
Liste unidimensionnelle
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
Liste unidimensionnelle
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
Liste unidimensionnelle
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample_list.reverse()
print("reversed list: ",sample_list)
Liste unidimensionnelle
sample_list = [5,4,3,2,1]
print("sample list: ",sample_list)
sorted_list = sorted(sample_list)
print("sorted list: ",sorted_list)
Liste unidimensionnelle
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sorted_list = sorted(sample_list,reverse=True)
print("sorted list: ",sorted_list)
Liste unidimensionnelle
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)
Liste unidimensionnelle
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)
Liste unidimensionnelle
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)
Liste unidimensionnelle
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)
Liste unidimensionnelle
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)
Liste unidimensionnelle
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)
Liste unidimensionnelle
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)
Liste unidimensionnelle
sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sum_value = sum(sample_list)
print("Sum Value: ",sum_value)
Recommended Posts