I want to change the order of the lists in a list depending on the matches of each element in the lists and also depending on how many matches there are. So if a string occurs in two lists, they should be ordered after one another. The more matches there are, the higher the priority for them to be after one another.

However, the first element of each list should not be considered in that ordering.

Example:

lst = [['title1', 'Anna', 'Tim', 'Bob', 'Victor'], ['title2', 'Kate', 'Lisa', 'Pam', 'Bob', 'Eva', 'Max'], ['title3', 'Anna', 'Tim', 'Kate', 'Ruth'], ['title4', 'Ian', 'Ivy', 'Lu', 'Victor']]

should be:

lst = [['title1', 'Anna', 'Tim', 'Bob', 'Victor'], ['title3', 'Anna', 'Tim', 'Kate', 'Ruth'], ['title2', 'Kate', 'Lisa', 'Pam', 'Bob', 'Eva', 'Max'], ['title4', 'Ian', 'Ivy', 'Lu', 'Eva']]

My approach is to start with Anna, count all her matches in the next lists and save the number of matches in a list with the length of how many lists follow. Then do the same with Tim... In this example the result of the first matches_count list should be: [1, 2, 1]. That means the list with 'title3' should follow next. Then changing the order and do these same steps with the following list that was put there previously.

Until now, I was trying to find the number matches with this code (changing of the order would be the next step, after figuring this out):

for i in range(len(lst)-1):
    matches_count = [0] * len(lst[i+1:])
    for j in range(1, len(lst[i])):
        for k in range(len(lst)-i-1):
            nextList = lst[k+1]
            currentName = lst[i][j]
            if currentName in nextList:
                matches_count[k] = matches_count[k] + 1
    print(matches_count)

The first list is right, the rest does not seem to be correct. It is:

[1, 2, 1]
[6, 1]
[1]
0

There are 0 best solutions below