Replace multiple substrings of a dictionary

Asked

Viewed 45 times

0

I’m trying to make a replace, but I’m not getting it.

My code:

dicionario = {
    "uva":"R$ 5,00", 
    "abacaxi": "15%", 
    "abacate": "preço: N/A"
    }

for key, value in dicionario.items():
  if 'R$ ' in value or '%' in value or 'N/A' in value:
    dicionario[key] = value.replace('R$ ', 'reais ')
    dicionario[key] = value.replace('%', 'porcento')
    dicionario[key] = value.replace('N/A', 'Nada')

print(dicionario)

Current output: {'uva': 'R$ 5,00', 'abacaxi': '15%', 'abacate': 'preço: Nada'}

Output that I desire: {'uva': 'reais 5,00', 'abacaxi': '15porcento', 'abacate': 'preço: Nada'}

For some reason I couldn’t solve, only the latest dictionary value is being updated. Does anyone have any light on where I’m wrong?

  • 1

    Perform each substitution on the previous result.

1 answer

3


First let’s modify your example a little to better understand what happens:

dicionario = {
    "uva":"R$ 5,00"
}

for key, value in dicionario.items():
    print('substituindo', value)
    if 'R$ ' in value or '%' in value or 'N/A' in value:
        print('antes da primeira substituição: ', value)
        dicionario[key] = value.replace('R$ ', 'reais ')
        print('depois da primeira substituição: ', dicionario[key])
        print('antes da segunda substituição: ', value)
        dicionario[key] = value.replace('%', 'porcento')
        print('depois da segunda substituição: ', dicionario[key])
        print('antes da terceira substituição: ', value)
        dicionario[key] = value.replace('N/A', 'Nada')
        print('depois da terceira substituição: ', dicionario[key])

I left the dictionary with only one entry, to simplify. Then, I print the value before each call from replace and the value of dicionario[key] right after. The result is:

substituindo R$ 5,00
antes da primeira substituição:  R$ 5,00
depois da primeira substituição:  reais 5,00
antes da segunda substituição:  R$ 5,00
depois da segunda substituição:  R$ 5,00
antes da terceira substituição:  R$ 5,00
depois da terceira substituição:  R$ 5,00

Note that the value never changes, because the call of replace does not modify the string: what it does is return another modified string.

Then you first exchange the "R$" for "real", and modify the value of the dictionary (dicionario[key] receives the value of the modified string, but value continues to have the "R$").

Then you try to trade the "%" for "percent," but how value no "%", it is not modified. And this unmodified value (still with "R$") is placed in dicionario[key] - that is, the previous value ("real 5.00") was superscripted, and dicionario[key] became "R$ 5,00".


If the idea is to make all replacements, the calls from replace should be chained. Something like that:

dicionario = {
    "uva":"R$ 5,00", 
    "abacaxi": "15%", 
    "abacate": "preço: N/A"
}

for key, value in dicionario.items():
    if 'R$ ' in value or '%' in value or 'N/A' in value:
        dicionario[key] = value.replace('R$ ', 'reais ').replace('%', ' porcento').replace('N/A', 'Nada')

print(dicionario)

But I think it only makes sense if a string can have more than one of these elements.
For example, if you have the string R$ 1,00 - 10%, she would become reais 1,00 - 10 porcento.

But if each string can only have one of these options (either it has only "R$", or it only has "%", etc), then it would not need to exchange all the possibilities, it would be enough to change only what it has:

# no caso de uma string só poder ter uma das opções
for key, value in dicionario.items():
    if 'R$ ' in value:
        dicionario[key] = value.replace('R$ ', 'reais ')
    elif '%' in value:
        dicionario[key] = value.replace('%', ' porcento')
    elif 'N/A' in value:
        dicionario[key] = value.replace('N/A', 'Nada')
  • Thank you very much, I could understand well where the problem was! abs =]

Browser other questions tagged

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