数组去重
2021-01-23 04:12
标签:int not list 列表 出现 循环 for循环 set 数组 数组去重 标签:int not list 列表 出现 循环 for循环 set 数组 原文地址:https://www.cnblogs.com/Gary1221/p/12885918.htmllist1 = [
["三国演义", "西游记", "红楼梦"],
["三国演义", "西游记", "红楼梦"],
["三国演义", "西游记", "水浒传"],
["三国演义", "西游记", "水浒传"],
["三国演义", "西游记", "论语"],
[1, 2, 3],
[1, 2, 3],
[5, 6, 8]
]
# 单纯的对列表进行去重,采用set()就能解决。但是用set()给数组去重则会出现问题。
# 方法一,无序,列表推导
list2 = [list(i) for i in set(tuple(j) for j in list1)]
print(list2)
# 方法二,如果需求是去重后按照顺序展示,采用for循环
list3 = list()
for i in list1:
if i not in list3:
list3.append(i)
print(list3)
上一篇:python知识3
下一篇:冒泡排序和选择排序的Java实现