Run a dict until empty in Python

0

I would like to go through as long as a list composed of values and keys is nonzero, just add values (values) in a list? The code I have is this:

hey = ['item1', 'item2', 'item3', 'item4']
print("hey", hey)
lol = [32, 54, 56, 64]
print("lol", lol)
lil = dict(zip(lol, hey))
print("lil: ",lil)
pop = list(zip(lil.values(), lil.keys()))
print("pop sem ordenar: ", pop)

sorted(pop, reverse=True)
print("pop ordenado: ", pop)

#for i in lil:
     #  print("i: ",i)
     #  print("lil[i]: ", lil[i])

max_value = max(pop)

print("max_value: ", max_value)
novo_lil = lil[max_value[1]]
del lil[max_value[1]]
print("lil nova: ",lil)

I tried to do this, but it did not work:

hey = ['item1', 'item2', 'item3', 'item4']
print("hey", hey)
lol = [32, 54, 56, 64]
print("lol", lol)
lil = dict(zip(lol, hey))
print("lil: ",lil)
pop = list(zip(lil.values(), lil.keys()))
print("pop sem ordenar: ", pop)

sorted(pop, reverse=True)
print("pop ordenado: ", pop)

#for i in lil:
     #  print("i: ",i)
     #  print("lil[i]: ", lil[i])

max_value = max(pop)

while not(lil[max_value[1]]):
    print("max_value: ", max_value)
    novo_lil = lil[max_value[1]]
    del lil[max_value[1]]
    print("lil nova: ",lil)

Can anyone help me? What I'm actually doing is sorting a dict and then I just add the values of it into a list, just that.

    
asked by anonymous 15.07.2016 / 21:16

2 answers

3

Our - - a lot of complacency for something that gets pretty simple in Python.

Dictionaries have the keys , values , and items methods that allow access to your members, and the powerful list-comprehension construct, which lets you apply any expression to each item in a sequence to generate a list .

So just do it:

hey = ['item1', 'item2', 'item3', 'item4']
lol = [32, 54, 56, 64]

dct = dict(zip(lol, hey))
result = [valor for chave, valor in sorted(dct.items(), reverse=True)]

Where dct is a dictionary that lists the two lists, as you have done - here we go through all the items in that dictionary already ordered by the key in the "sorted" call - how you want them in descending order, ". And from each item, we get the second value - which is the value associated with the first original list.

    
19.01.2017 / 22:05
0

To sort a list according to the other I developed the following solution:

hey = ['item1', 'item2', 'item3', 'item4']
lol = [32, 54, 56, 64]

''' vamos cria uma lista de transposição com a posição original
e a nova posição do item. '''
transposicao = []
for posicao_nova, i in enumerate(sorted(lol, reverse=True)):
    posicao_original = lol.index(i)
    transposicao.append([posicao_nova, posicao_original])

# após guardar as posições, efetivamente alteramos a lista lol
lol = sorted(lol, reverse=True)

# agora utilizamos a lista de transposição para reordenar a lista hey
nova_hey = []
for i in transposicao:
    nova_hey.append(hey[i[1]])

hey = nova_hey

print(hey, lol)

The result will be as desired: ['item4', 'item3', 'item2', 'item1'] [64, 56, 54, 32]

A simpler and more "pythonic" version:

from operator import itemgetter

hey = ['item1', 'item2', 'item3', 'item4']
lol = [32, 54, 56, 64]

# junta as listas e ordena de acordo com os valors da segunda lista     (lol no caso)
ordena_junto = sorted(list(zip(hey, lol)), key=itemgetter(1), reverse=True)

# extrai as listas ordernadas
hey, lol = zip(*ordena_junto)
print(hey, lol)

Result: ('item4', 'item3', 'item2', 'item1') (64, 56, 54, 32)

I hope it helped solve your problem.

    
19.01.2017 / 21:48