Sort elements of a python dictionary

Asked

Viewed 948 times

-1

Hello, I have a csv file, from which I read to a dictionary, the file is as follows:

maquina,num,projeto,var,reg,nome,valor
br,1,1809123,float,4637,teste1
br,1,1809123,float,4639,teste2
br,2,1809123,float,4641,teste1
br,2,1809123,float,4643,teste2
br,2,1809123,int,4107,teste3
al,1,1809123,int,4186,teste4
al,2,1809123,float,4645,teste4

My dictionary looks like this(with the acquisition of values):

OrderedDict([('maquina', 'br'), ('num', '1'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4637'), ('nome', 'teste1'), ('valor', 1.1869791746139526)])
OrderedDict([('maquina', 'br'), ('num', '1'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4639'), ('nome', 'teste2'), ('valor', 0.1163330078125)])
OrderedDict([('maquina', 'br'), ('num', '2'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4641'), ('nome', 'teste1'), ('valor', 1077.43994140625)])
OrderedDict([('maquina', 'br'), ('num', '2'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4643'), ('nome', 'teste2'), ('valor', 124.00007629394531)])
OrderedDict([('maquina', 'br'), ('num', '2'), ('projeto', '1809123'), ('var', 'int'), ('reg', '4107'), ('nome', 'teste3'), ('valor', 228)])
OrderedDict([('maquina', 'al'), ('num', '1'), ('projeto', '1809123'), ('var', 'int'), ('reg', '4186'), ('nome', 'teste4'), ('valor', 250)])
OrderedDict([('maquina', 'al'), ('num', '2'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4645'), ('nome', 'teste4'), ('valor', 0.0)])

Now I need to sort the data to later record it in a database, so I created the following function:

def preparar_dados(dados_modbus,maquina,num):
registradores = {}
for dado in dados_modbus:
    if dado['maquina'] == maquina and dado['num'] == num:
        registradores.update({dado['maquina']:dado['num'],dado['nome']:dado['valor']})
return registradores

In this function I provide the dictionary, the name of the machine and its number. But I’d like the show to do that without me having to tell him the name and one of the machine. I want a logic to change the function itself to not need to inform "host" and "one". The result of this new code would be the following:

{'al': '1', 'teste6': 0}
{'br': '1', 'teste1': 0.952343761920929, 'teste2': 0.0997314453125}
{'al': '2', 'teste7': 0.0}
{'br': '2', 'teste3': 1077.43994140625, 'teste4': 104.00007629394531, 'teste5': 0}

I look forward to some help in this matter.

  • What is the ordination rule?

  • "machine" and "num" are the values that combined should be unique, at the moment I am informing them to the function manually, but I really wanted the program to do this automatically. I don’t know if ordering is the correct word, I need the function to return as a result the values I put at the end of the question. No need for this here: dados_br1 = preparar_dados(dados_modbus,"br","1")
 dados_al1 = preparar_dados(dados_modbus,"al","1") It should be something like this: dados_ordenados = preparar_dados(dados_modbus)

1 answer

1


First I created a list of the dictionaries you put up.

lista = [
OrderedDict([('maquina', 'br'), ('num', '1'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4637'), ('nome', 'teste1'), ('valor', 1.1869791746139526)]),
OrderedDict([('maquina', 'br'), ('num', '1'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4639'), ('nome', 'teste2'), ('valor', 0.1163330078125)]),
OrderedDict([('maquina', 'br'), ('num', '2'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4641'), ('nome', 'teste1'), ('valor', 1077.43994140625)]),
OrderedDict([('maquina', 'br'), ('num', '2'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4643'), ('nome', 'teste2'), ('valor', 124.00007629394531)]),
OrderedDict([('maquina', 'br'), ('num', '2'), ('projeto', '1809123'), ('var', 'int'), ('reg', '4107'), ('nome', 'teste3'), ('valor', 228)]),
OrderedDict([('maquina', 'al'), ('num', '1'), ('projeto', '1809123'), ('var', 'int'), ('reg', '4186'), ('nome', 'teste4'), ('valor', 250)]),
OrderedDict([('maquina', 'al'), ('num', '2'), ('projeto', '1809123'), ('var', 'float'), ('reg', '4645'), ('nome', 'teste4'), ('valor', 0.0)])
]

Then you can go through the whole list and arrange the way you suggested without having to inform the machine and number

registro = []
temp = []
for lst in lista:
    if (lst['maquina'], lst['num']) in temp:    
        count = temp.index((lst['maquina'], lst['num']))
        registro[count].update({lst['nome']:lst['valor']})
    else:
        temp.append((lst['maquina'], lst['num']))
        registro.append({})
        count = temp.index((lst['maquina'], lst['num']))        
        registro[count][lst['maquina']] = lst['num']
        registro[count][lst['nome']] = lst['valor']       

Finally to maintain the order of the dictionary you can do as follows:

temp_sort = sorted(temp, key=lambda x: x[0])
for ts in temp_sort:
    count = temp.index(ts)
    print(registro[count])

The print is as follows:

{'al': '1', 'teste4': 250}
{'al': '2', 'teste4': 0.0}
{'br': '1', 'teste1': 1.1869791746139526, 'teste2': 0.1163330078125}
{'br': '2', 'teste1': 1077.43994140625, 'teste2': 124.00007629394531, 'teste3': 228}
  • Very good.... exactly what I need, I will study what each part does, because I am new to python. It helped me a lot;

Browser other questions tagged

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