This is a code to sort a list of lists (matrix of data) based on the names on second column (column 1) in a way equal names will be at the biggest possible distance of each other inside of the sorted matrix of data.
This subject has been discussed here and here seeking for a solution in Excel and Python, respectively. Despite of the good proposed solutions, none of them worked with my real data, and I worked-out this code that I am sharing with you. Feel free to comment, criticize and improve, if you like. It has a lot of print
that are only for a better visualization of the steps. It worked well in the various tests I did so far.
import datetime
matrix = [[True, 'Tricha', 6, 'country', 'state', 'city', 'username', datetime.datetime(2020, 10, 25, 21, 0), 'comment'], [True, 'Joe', 6, 'country', 'state', 'city', 'username', datetime.datetime(2020, 10, 25, 21, 0), 'comment'], [True, 'July', 6, 'country', 'state', 'city', 'username', datetime.datetime(2020, 10, 25, 21, 0), 'comment'], [True, 'John', 8, 'country', 'state', 'city', 'username', datetime.datetime(2020, 10, 25, 21, 0), 'comment'], [True, 'John', 8, 'country', 'state', 'city', 'username', datetime.datetime(2020, 10, 25, 21, 0), 'comment'], [True, 'John', 8, 'country', 'state', 'city', 'username', datetime.datetime(2020, 10, 25, 21, 0), 'comment'], [True, 'Mary', 7, 'country', 'state', 'city', 'username', datetime.datetime(2020, 10, 25, 21, 0), 'comment'], [True, 'Mary', 7, 'country', 'state', 'city', 'username', datetime.datetime(2020, 10, 25, 21, 0), 'comment'], [True, 'Mike', 9, 'country', 'state', 'city', 'username', datetime.datetime(2020, 10, 25, 21, 0), 'comment'], [True, 'Mike', 9, 'country', 'state', 'city', 'username', datetime.datetime(2020, 10, 25, 21, 0), 'comment'], [True, 'Bob', 7, 'country', 'state', 'city', 'username', datetime.datetime(2020, 10, 25, 21, 0), 'comment']]
column_to_sort = 1 # column of the element that will be used to sort the matrix
lista = []
for item in matrix:
lista.append(item[column_to_sort])
print(lista)
lista2 = []
for name in lista:
if name not in lista2:
lista2.append(name)
print (lista2)
dic = {}
for item in lista2:
dic[item] = 0
print(item)
print (f'dic with quantity equal to zero: {dic}')
for name in lista:
n = dic[name]
n = n + 1
dic[name] = n
print (f'dic: {dic}')
# sort the names according to the number of times it appears in dic
dic2 = sorted(dic.items(), key=lambda x: x[column_to_sort])
print (f'dic2: {dic2}')
matrix2 = []
len_matrix = len(matrix)
for g in range (1, len_matrix+1):
print(f'g: {g}')
for item in dic2:
if item[column_to_sort] == g:
position = 0
for i in range(0, len_matrix):
print(f'i: {i}')
print(f'g: {g} matrix2 len: {len(matrix2)}')
if item[0] == matrix[i][column_to_sort]:
print(f'Go to position: {position} for {matrix[i]}')
matrix2.insert(position, matrix[i])
position = int(position + len(matrix2) / g) + 1
print (f'matrix2: {matrix2}')
In my case it is used to rotate the vendor of digital information in a way that, if one vendor ("name") fail, then it is rotated to a different vendor, and will come back to the failed vendor at the latest possible time (that may give the vendor time enough to fix the issue).