Create a dictionary from a list

Asked

Viewed 4,772 times

1

I have the following list:

msg = ['CAD400','Uma Filial...','Solucao:','O campo...','','LIB310','A Filial...','Solucao:','Foi identificado...','Foi corrigido...','','PAG302','Mudanca...','Solucao:','O erro...','O programa...','Programa alterado...']

I want a dictionary with the list data as follows:

msg_nova = {'CAD_001': ['Uma Filial...','Solucao:','O campo...'],'REC_002': ['A Filial...','Solucao:','Foi identificado...','Foi corrigido...'],'PAG_003': ['Mudanca...','Solucao:','O erro...','O programa...','Programa alterado...']}

I have tried several ways and I could not make the descriptions of each program (CAD, REC, PAG) be linked to each one respectively. I even managed to create the keys but the descriptions are each separate in a list.

  • 1

    I don’t see how the initial list reaches the proposed result. Because ['A Filial...' is associated with REC_002 and not LIB310 ? Explain better how values are associated.

1 answer

3


You can scan the list by scanning items prefixed with CAD, PAG or LIB. If the item has any of the prefixes, a new key is included in a dictionary and its value (a list) is filled in until a new key is found.

Follows a possible solution:

msg = [ 'CAD400','Uma Filial...','Solucao','O campo...','','LIB310','A Filial...','Solucao','Foi identificado...','Foi corrigido...','','PAG302','Mudanca...','Solucao','O erro...','O programa...','Programa alterado...']

prfx = ['CAD','PAG','LIB']
dic = {}
key = ''

for m in msg:
    for p in prfx:
        if p in m:
            key = m
            dic[key] = []
            break
    if m != key:
        dic[key].append(m)

print(dic)

Exit:

{'LIB310': ['A Filial...', 'Solucao', 'Foi identificado...', 'Foi corrigido...', ''],
 'PAG302': ['Mudanca...', 'Solucao', 'O erro...', 'O programa...', 'Programa alterado...'],
 'CAD400': ['Uma Filial...', 'Solucao', 'O campo...', '']}
  • Lacobus, almost perfect brother, I say almost because I haven’t had a little time to identify why it is not coming out organized in the dictionary the order in which it is in the list, the key CAD400 is in the last sequence. Anyway it’s already pretty good.

  • @NJAGO: It is not possible to control the order of pairs (key/value) within a dictionary. However, you can convert your dictionary to a list of tuplas to make this ordering possible. Reference: link.

  • Perfect @Lacobus, thank you so much.

  • 1

    As @Lacobus said, the dict does not guarantee the order of the keys, but it is possible to use Ordereddict along with sorted to create an orderly dictionary.

  • @Thomazsoares: O OrderedDict implements the technique of converting to tuplas to carry out the ordination.

  • @Lacobus, the difference is OrderedDict is a subclass of dict and has all its functions. IE, da para acesso os valores por chave dic['CAD400'] or dic.get(''CAD400').

Show 1 more comment

Browser other questions tagged

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