问题:
有一个列表[11, 2, 3, 3, 7, 9, 11, 2],去重并且保持原来的顺序。
1 """ 2 有一个列表[11, 2, 3, 3, 7, 9, 11, 2, 3],去重并且保持原来的顺序. 3 """ 4 5 l1 = [11, 2, 3, 3, 7, 9, 11, 2, 3] 6 ret = list(set(l1)) 7 print(ret) 8 ret.sort(key=l1.index) # 按值在l1中的索引进行排序 9 print(ret) 10 11 12 l2 = [ 13 {"name": "alex", "age": 36}, 14 {"name": "GoldBoss", "age": 30}, 15 {"name": "xiaoyima", "age": 18} 16 ] 17 # 课后作业 18 # 以年龄来排序: 19 l2.sort(key=lambda x: x["age"]) 20 print(l2)
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/18182.html