Compare values in a list of tuples [duplicate]

0
 lista  = [('Maria', 28, 7.0), ('Ana', 30, 9.0), ('Thiago', 30, 9.0)]
 resultado = []
 for i in lista:
    if i[2]<i[2]+1:
        resultado.append(i[0])
 print(resultado)

Correct output: Ana, Thiago, Maria

My output is Maria, Ana , Thiago . What gives us that was not made the comparison with the average values and then age and no name. I would like a help how can I compare the values of this list and print in order, first comparison by average if average is equal step for age if age is equal step for name, I am two weeks trying to solve this problem I am having a lot of difficulties

    
asked by anonymous 09.04.2018 / 21:03

2 answers

4

Two weeks is a long time, since you can solve in 1/2 lines d:

lista = [('Thiago', 30, 9.0), ('Maria', 28, 7.0), ('Ana', 30, 9.0)]
ordered = sorted(lista, key=lambda x: (x[2], x[1], x[0]))
print(ordered) # [('Maria', 28, 7.0), ('Ana', 30, 9.0), ('Thiago', 30, 9.0)]

STATEMENT

If you want in reverse order:

...
ordered = sorted(lista, key=lambda x: (x[2], x[1], x[0]), reverse=True)
# [('Thiago', 30, 9.0), ('Ana', 30, 9.0), ('Maria', 28, 7.0)]
...

The trick is in the argument key whose value is a function in which you can define by which value (s) to sort.

DOCS

    
09.04.2018 / 22:12
2

In addition to Miguel's response, I'll show you how to implement the ordering that you want "on hand." It should be noted that in a real situation there is no need to do this for several reasons, and an implementation of it will most likely be much less efficient than the native implementation already provided (as I demonstrate in this answer).

For simplicity I have chosen the Selection Sort , remembering that other algorithms will be more efficient such as < in> QuickSort , MergeSort , HeapSort among others that guarantee time complexity in the order of O (nlogn).

Example:

lista = [('Thiago', 30, 9.0), ('Maria', 28, 7.0), ('Ana', 30, 9.0)]
resultado = [i for i in lista]  # passar tudo da lista para resultado
tamanho = len(resultado)  # achar e guardar o tamanho

for i in range(0, tamanho):  # para cada posição da lista
    menor = i  # pre-definir o menor como o elemento corrente
    for j in range(i + 1, tamanho):  # percorrer os restantes para achar o menor
        # se este é menor    
        if resultado[j][2] < resultado[menor][2] or \
                (resultado[j][2] == resultado[menor][2] and resultado[j][1] < resultado[menor][1]) or \
                (resultado[j][1] == resultado[menor][1] and resultado[j][0] < resultado[menor][0]):
            menor = j  # guarda a posição

    if resultado[i] != resultado[menor]:  # se achou um menor diferente do corrente
        # faz a troca de posição entre o corrente e o menor
        temp = resultado[menor]
        resultado[menor] = resultado[i]
        resultado[i] = temp

print(resultado)

The if within the second for is what defines the way the elements are ordered because it is what defines the least element for that position. Note that I started by comparing the [j][2] position, ie in the current element, the 2 position that corresponds to the average:

if resultado[j][2] < resultado[menor][2]

If it is not smaller but equal, it will now compare with another value of the same element, the age at position 1 :

or \
(resultado[j][2] == resultado[menor][2] and resultado[j][1] < resultado[menor][1])

And if the position 1 is the same compares by the name that is in the 0 position:

or \
(resultado[j][1] == resultado[menor][1] and resultado[j][0] < resultado[menor][0]):

See the Ideone example

    
10.04.2018 / 00:57