输入:
[yellow, red, green, blue]
{green:go, yellow:attention, red:stay}
如何制作新列表:
[attention, stay, go, blue]
有没有办法用 lambda 来实现?
l1 = ['yellow', 'red', 'green', 'blue']
dict1 = {'green':'go', 'yellow':'attention', 'red':'stay'}
list(map(lambda x: dict1.get(x, x), l1))
输出:
['attention', 'stay', 'go', 'blue']
[dict1.get(x, x) for x in l1]
输出:
['attention', 'stay', 'go', 'blue']
[*map(dict1.get, l1, l1)]
输出:
['attention', 'stay', 'go', 'blue']