假设我有这些列表:
ids = [4, 3, 7, 8]
objects = [
{"id": 7, "text": "are"},
{"id": 3, "text": "how"},
{"id": 8, "text": "you"},
{"id": 4, "text": "hello"}
]
如何对objects
进行排序,使其id的顺序与ids
匹配?例如,要获得以下结果:
objects = [
{"id": 4, "text": "hello"},
{"id": 3, "text": "how"},
{"id": 7, "text": "are"},
{"id": 8, "text": "you"}
]
4条答案
按热度按时间kuuvgm7e1#
6yt4nkrj2#
z8dt9xmd3#
bbmckpt74#
将
sorted
与自定义key
函数一起使用,该函数只是从ids
获取index
:(灵感来自NPE’s answer。)