Answer the question
In order to leave comments, you need to log in
How to simplify code that replaces values in a dictionary?
How can the following code be simplified?
if r['opcode'] == 1:
r['opcode'] = str(r['opcode']).replace('1', 'Оплата')
if r['opcode'] == 2:
r['opcode'] = str(r['opcode']).replace('2', 'Возврат')
if r['opcode'] == 3:
r['opcode'] = str(r['opcode']).replace('3', 'Рекуррентное списание')
if r['opcode'] == 4:
r['opcode'] = str(r['opcode']).replace('4', 'Блокировка')
Answer the question
In order to leave comments, you need to log in
d = {
1: 'Оплата',
2: 'Возврат',
3: 'Рекуррентное списание',
4: 'Блокировка',
}
r['opcode'] = d[r['opcode']]
To change the value in the dictionary, you can simply:
if r['opcode'] == 1:
r['opcode'] = 'Payment'
r['opcode'] = ['Оплата', 'Возврат', 'Рекуррентное списание', 'Блокировка'][r['opcode'] - 1]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question