我想根据字典的值映射 python 列表。当我有 pandas 系列时,我知道该怎么做,但我不知道如何使用列表:
l = ['apple', 'lemmon', 'banana']
x = {'apple':1, 'lemmon':2, 'banana':3, 'chery':4}
l = l.map(x)
print(l)
期望的输出:
l = [1,2,3]
最佳答案
这可以通过 Python 列表理解来解决。 在你的情况下:
l = [x[key] for key in l]
请注意,l
中的所有元素都需要作为 x
中的键存在。
关于python - 基于字典值映射Python列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67476143/