Python删除二维数组中的重复元素

class Solution:
    def removeDuplicates(self, nums) :
        # 对列表进行循环修改时要使用nums[:]而不是nums
        for n in nums:
            if nums.count(n) > 1:
                nums.remove(n)
        return nums



s=Solution()
List=[]
Length=4
for i in range(Length):
    List.append([])#初始化二维列表
List[0].append(1)
List[0].append(2)
List[1].append(1)
List[1].append(3)
List[2].append(1)
List[2].append(2)
List[3].append(1)
List[3].append(3)
print(List)
com=s.removeDuplicates(List)
print(com)

输出:

[[1, 2], [1, 3], [1, 2], [1, 3]]
[[1, 2], [1, 3]]

这种方法只能去除重复两次的元素。

猜你喜欢

转载自blog.csdn.net/qq_43511299/article/details/114192780