0
How do I go through while a list of values and keys is non-zero, just add the 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 it, but it didn’t 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 someone help me? What I’m actually doing is ordering a Dict and then adding only its values to a list, that’s all.
I didn’t understand your doubt, but I realized that you are using the Sorted function in the wrong way, you should do so "pop = Sorted(pop,Reverse=True)". If you can explain the doubt better.
– Paulo Henrique Cardoso
Okay @Paulohenrirdoso, it’s like this: I have two lists. Each value of hey corresponds to a value of lol. Soon 'item1' has value 32, 'item2' has value 54 and so on! I need to sort the list hey based on the list lol in descending form, so lol stay: [64, 56, 54, 32] but the list hey should also be ordered corresponding with each value of lol: hey = ['Item4', 'item3', 'item2', 'item1']
– Allan
The way I found my friend @Miguel was to relate them through a Dict as it is done in: Lil = Dict(zip(lol, hey))
– Allan
So I turn this Dict of mine into a list and I order it, like you corrected it. Since it is not possible to order a Dict, I thought to take the higher value of Dict and add it in order in a list, but only its values()
– Allan
So that at the end I have hey = ['Item4', 'item3', 'item2', 'item1'] ordered based on the lol list, I managed to explain?
– Allan
It’s just that it’s a little hard to really explain, I apologize for that.
– Allan
I don’t know if I understand yet, but your problem seems to be the fact that the dictionary is not ordered, so you can use the Ordered Dict of the Collections library. But see if these two lines of code after sorting the "pop" list solve your problem :for i in range(Len(hey)): hey[i] = pop[i][0]
– Paulo Henrique Cardoso
please pot, answer the question - ("statement"). Currently you have nothing in common with the problem stated.
– jsbueno