1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #! python3
# coding: utf-8
import numpy as np
x = [[0, 3, 3], [0, 7, 4], [0, 8, 1], [0, 10, 2], [1, 11, 3],
[2, 12, 1], [3, 13, 3], [4, 14, 4], [5, 15, 2], [6, 16, 3],
[7, 17, 4], [8, 9, 1], [9, 18, 1], [10, 20, 2], [11, 21, 3],
[12, 21, 1], [13, 6, 3], [14, 21, 4], [15, 21, 2], [16, 1, 3],
[17, 4, 4], [18, 19, 1], [19, 2, 1], [20, 5, 2]]
array = np.array(x)
# print(array)
index = array[:, 2].argsort()
array_index = array[index]
print(array_index)
unique, counts = np.unique(array_index[:, 2], return_counts=True)
dict_index = dict(zip(unique, counts))
print(dict_index)
n1 = 0
n2 = dict_index[1]
array_1 = np.array([(array_index[n1:n2, 0]), (array_index[n1:n2, 1])]).flatten()
n1 = n2
n2 += dict_index[2]
array_2 = np.array([(array_index[n1:n2, 0]), (array_index[n1:n2, 1])]).flatten()
n1 = n2
n2 += dict_index[3]
array_3 = np.array([(array_index[n1:n2, 0]), (array_index[n1:n2, 1])]).flatten()
n1 = n2
n2 += dict_index[4]
array_4 = np.array([(array_index[n1:n2, 0]), (array_index[n1:n2, 1])]).flatten()
print(array_1, np.sort(array_1))
print(array_2, np.sort(array_2))
print(array_3, np.sort(array_3))
print(array_4, np.sort(array_4))
"""
[[ 8 9 1]
[18 19 1]
[ 0 8 1]
[12 21 1]
[ 2 12 1]
[ 9 18 1]
[19 2 1]
[15 21 2]
[10 20 2]
[20 5 2]
[ 5 15 2]
[ 0 10 2]
[ 3 13 3]
[11 21 3]
[ 1 11 3]
[13 6 3]
[16 1 3]
[ 6 16 3]
[ 0 3 3]
[ 7 17 4]
[ 4 14 4]
[14 21 4]
[17 4 4]
[ 0 7 4]]
{1: 7, 2: 5, 3: 7, 4: 5}
[ 8 18 0 12 2 9 19 9 19 8 21 12 18 2] [ 0 2 2 8 8 9 9 12 12 18 18 19 19 21]
[15 10 20 5 0 21 20 5 15 10] [ 0 5 5 10 10 15 15 20 20 21]
[ 3 11 1 13 16 6 0 13 21 11 6 1 16 3] [ 0 1 1 3 3 6 6 11 11 13 13 16 16 21]
[ 7 4 14 17 0 17 14 21 4 7] [ 0 4 4 7 7 14 14 17 17 21]
""" |
Partager