Python二维列表去重,有两种方法:使用集合:
Python二维列表去重,有两种方法:
1. 使用集合:
python
# 定义一个二维列表
list_2d = [[1, 2], [3, 4], [5, 6], [1, 2]]
# 将二维列表转换成集合
set_2d = set(map(tuple, list_2d))
# 将集合转换成二维列表
list_2d_unique = list(map(list, set_2d))
# 打印结果
print(list_2d_unique)
# [[1, 2], [3, 4], [5, 6]]
2. 使用列表推导式:
python
# 定义一个二维列表
list_2d = [[1, 2], [3, 4], [5, 6], [1, 2]]
# 使用列表推导式进行去重
list_2d_unique = [list(x) for x in set(tuple(x) for x in list_2d)]
# 打印结果
print(list_2d_unique)
# [[1, 2], [3, 4], [5, 6]]