Python:对数据矩阵(列表列表)进行排序,使其在彼此最大距离处具有相同的“名称”

2024-03-20

这是一个代码,用于根据第二列(第 1 列)上的名称对列表列表(数据矩阵)进行排序,相同的名称将在排序的数据矩阵内彼此之间具有最大可能的距离。

这个话题已经讨论过了here https://stackoverflow.com/questions/75249961/how-to-sort-rows-in-excel-without-having-repeated-data-together and here https://stackoverflow.com/questions/75442192/how-to-sort-a-list-of-lists-in-python-without-having-repeated-names-together分别在 Excel 和 Python 中寻求解决方案。尽管提出了很好的解决方案,但它们都不适用于我的真实数据,并且我编写了与您分享的代码。如果您愿意,请随意评论、批评和改进。它有很多print这只是为了更好地可视化步骤。它在我迄今为止所做的各种测试中运行良好。


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}')



就我而言,它用于轮换数字信息供应商,如果一个供应商(“名称”)失败,则将其轮换到另一供应商,并将在最晚可能的时间返回到失败的供应商(这可能会给供应商足够的时间来解决问题)。


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python:对数据矩阵(列表列表)进行排序,使其在彼此最大距离处具有相同的“名称” 的相关文章

随机推荐