"rank" array in Python

Asked

Viewed 277 times

2

I’m having a problem and I’m having trouble solving it, the following is I have a list of the kind :

['Words', 4], ['evidenced', 3], ['presented', 2], ['showed', 2], ['second', 1], ['treaties', 1], ['realized', 1]

Where the first term is the word and the second the repetition of it in any text, the algorithm already returns me in descending order the number of repetitions now I need to rank, ie say that for example ['palavras',4] is the most repeated term, 1\176; and ['tratados', 1], ['realizada', 1] would be in this case the 4° placed (both in the same position because it has the same amount of repetitions) for example, I wanted if possible to have a return of type

['palavra','repetição','lugar do rankig'], I’m hours trying but I can’t imagine how to do.

Excerpt from the code where it is generated:

for key,value in reversed(sorted(self.frequency.items(),key=itemgetter(1))):
  self.palavras.append([key,value])

1 answer

3


Let’s start by sorting the list:

lista = [['segundo', 1], ['evidenciaram', 3],['Palavras', 4], ['apresentado', 2], ['tratados', 1], ['realizada', 1], ['mostraram', 2]]
l_sorted = sorted(lista, key=lambda x: x[1], reverse=True) # ordenamos pelo segundo elemento de cada sublista e invertemos a ordem
print(l_sorted) # [['Palavras', 4], ['evidenciaram', 3], ['apresentado', 2], ['mostraram', 2], ['segundo', 1], ['tratados', 1], ['realizada', 1]]

Now let’s try to add to each sublist its place in the ranking (comment explanations in the code below):

rank = 1
l_sorted[0].append(rank) # adicionamos o rank 1 a primeira sublista visto que nao vale a pena faze-lo dentro do ciclo
for i in range(len(l_sorted)-1):
    if(l_sorted[i][1] != l_sorted[i+1][1]): # comparamos as repeticoes currentes com as seguintes e adcionamos 1 ao rank caso sejam diferentes
        rank += 1
    l_sorted[i+1].append(rank) # adicionamos a sublista seguinte o rank correspondente
print(l_sorted) # [['Palavras', 4, 1], ['evidenciaram', 3, 2], ['apresentado', 2, 3], ['mostraram', 2, 3], ['segundo', 1, 4], ['tratados', 1, 4], ['realizada', 1, 4]]

Where the last element of each sublist is its place in the ranking.

Complete code:

lista = [['segundo', 1], ['evidenciaram', 3],['Palavras', 4], ['apresentado', 2], ['tratados', 1], ['realizada', 1], ['mostraram', 2]]
l_sorted = sorted(lista, key=lambda x: x[1], reverse=True)

rank = 1
l_sorted[0].append(rank)
for i in range(len(l_sorted)-1):
    if(l_sorted[i][1] != l_sorted[i+1][1]):
        rank += 1
    l_sorted[i+1].append(rank)
# l_sorted = [['Palavras', 4, 1], ['evidenciaram', 3, 2], ['apresentado', 2, 3], ['mostraram', 2, 3], ['segundo', 1, 4], ['tratados', 1, 4], ['realizada', 1, 4]]

DEMONSTRATION

  • 1

    Thank you very much, it worked here, hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.