I would like to ask you to change the dictionary value according to the conditions.

Asked 2 years ago, Updated 2 years ago, 59 views

data = {'sub': when you have ['s1', 's2', 's3', ...]}

I want to change it like this, what should I do?

//
data = {'sub': ['s1', 's2', 's3']}

for i in range(len(data)):
    if data['sub'][i] == 's1':
        data['sub'][i] = 's20'
     elif data['sub'][i] == 's2':
        data['sub'][i] = 's21'
     elif data['sub'][i] == 's3':
        data['sub'][i] = 's22'

I tried it in the same form as above, but it didn't work out, so I'd appreciate your help.

dictionary

2022-09-20 08:46

2 Answers

data = {'sub': ['s1', 's2', 's3']}

for i in range(len(data['sub'])):
    if data['sub'][i] == 's1':
        data['sub'][i] = 's20'
    elif data['sub'][i] == 's2':
        data['sub'][i] = 's21'
    elif data['sub'][i] == 's3':
        data['sub'][i] = 's22'


2022-09-20 08:46

data = {'sub': ['s1', 's2', 's3']}

for n, i in enumerate(data['sub']):
    if i == 's1':
        data['sub'][n] = 's20'
     elif i == 's2':
        data['sub'][n] = 's21'
     elif i == 's3':
        data['sub'][n] = 's22'


2022-09-20 08:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.