Pythonは2次元配列の重複要素を削除します

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]]

このメソッドは、2回繰り返される要素のみを削除できます。

おすすめ

転載: blog.csdn.net/qq_43511299/article/details/114192780
おすすめ