python面试,日更
l1 = [11, 2, 3, 22, 2, 4, 11, 3] 去重并保持原来顺序
# 集合方法 l2 = list(set(l1)) l2.sort(key=l1.index) # 按照l1索引排序 print(l2) # 列表 l3 = [] for item in l1: if item not in l3: l3.append(item) print(l3)答案
l4 = [ {"name":"lmj11","age":11}, {"name":"lmj44","age":44}, {"name":"lmj33","age":33}, {"name":"lmj77","age":77}, {"name":"lmj66","age":66}, {"name":"lmj55","age":55}, ]
# 使用lambda表达式 l4.sort(key= lambda x:x["age"]) # 方式一 l4=sorted(l4,key=lambda x:x["age"]) # 方式二 print(l4) # 其他方式 # def tmp(x): # return x["age"] # l4.sort(key=tmp) # print(l4)答案
待续。。。
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16