Replace Python list values? I want to know how to do it.

Asked 2 years ago, Updated 2 years ago, 14 views

updown_img = ['▲','▼']
a_list = ['1', '1', '1', '0', '0', '1', '1', '1', '0', '0'] 

There are two lists I'd like to change it to ▲ if it's 1 or ▼ if it's 0.

Desired output value

b_list = ['▲', '▲', '▲', '▼', '▼', '▲', '▲', '▲', '▼', '▼'] 

What should I do? I ask for your help me

python

2022-09-22 08:19

1 Answers

using map, lambda

b_list = map(lambda x: updown_img[int(x)^1], a_list)

using list comprehension

b_list = [updown_img[int(x)^1] for x in a_list]


2022-09-22 08:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.