标签:and 不能 list code ofo 索引 remove ids 排序
参考原文:https://www.the5fire.com/python-remove-duplicates-in-list.html需求:去list进行去重,去重后保证顺序不变
ids = [1, 2, 3, 3, 4, 2, 3, 4, 5, 6, 1]
new_ids = []
for id in ids:
if id not in new_ids:
new_ids.append(id)
print("new_ids==>", new_ids)
ids = [1,4,3,3,4,2,3,4,5,6,1]
new_ids = list(set(ids))
print(new_ids)
测试发现去重后不能保证原来的顺序
ids = [1, 4, 3, 3, 4, 2, 3, 4, 5, 6, 1]
new_ids = list(set(ids))
new_ids.sort(key=ids.index)
print(new_ids)
ids = [1,4,3,3,4,2,3,4,5,6,1]
func = lambda x,y:x if y in x else x + [y]
reduce(func, [[], ] + ids)
[1, 4, 3, 2, 5, 6]
其中的 lambda x,y:x if y in x else x + [y] 等价于 lambda x,y: y in x and x or x+[y] 。
思路其实就是先把ids变为[[], 1,4,3,......] ,然后在利用reduce的特性
标签:and 不能 list code ofo 索引 remove ids 排序
原文地址:http://blog.51cto.com/dzm911/2328543