"""
有一个列表[11, 22, 33, 33, 77, 99, 11, 22, 33],去重并且保持原来的顺序.
"""
ln = [11, 22, 33, 33, 77, 99, 11, 22, 33]
ret = list(set(ln))
print(ret)
-> [33, 99, 11, 77, 22]
ret.sort(key=ln.index) # 按值在ln中的索引进行排序
print(ret)
-> [11, 22, 33, 77, 99]
l2 = [
{"name": "xiaoming", "age": 25},
{"name": "xiaohong", "age": 24},
{"name": "xiaohei", "age": 23}
]
# 匿名函数
l2.sort(key=lambda x: x["age"]) #取列表中每个字典的"age"键对应的值进行排序
print(l2)
-> [{'name': 'xiaohei', 'age': 23}, {'name': 'xiaohong', 'age': 24}, {'name': 'xiaoming', 'age': 25}]