"rankear" array in Python

2

I have a problem and I can not solve it, it's the following I have a list of type list:

  

['Words', 4], ['evidenced', 3], ['presented', 2],   [1], [1], ['performed'], [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 amount of repetitions now I need to rank, ie say that ['palavras',4] is the term that most repeats or is it is the 1st and ['tratados', 1] , ['realizada', 1] would in this case be the 4th place (both in the same position because it has the same amount of repetitions) for example, I wanted if possible had a return of type

['palavra','repetição','lugar do rankig'] , I'm trying for hours but I can not figure out how to do it.

Excerpt of the code where it is generated:

for key,value in reversed(sorted(self.frequency.items(),key=itemgetter(1))):
  self.palavras.append([key,value])
    
asked by anonymous 22.09.2017 / 05:16

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 your place in the ranking (explanations in comment 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]]

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

    
22.09.2017 / 10:47