Python程序,用于移除矩阵中具有重复元素的行
如需要删除矩阵中具有重复元素的行,则使用列表解析和“set”运算符。
示例
以下是同一演示:
my_list = [[34, 23, 34], [17, 46, 47], [22, 14, 22], [28, 91, 19]] print("The list is :") print(my_list) my_result = [element for element in my_list if len(set(element)) == len(element)] print("The result is :") print(my_result)
输出
The list is : [[34, 23, 34], [17, 46, 47], [22, 14, 22], [28, 91, 19]] The result is : [[17, 46, 47], [28, 91, 19]]
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
说明
定义了一个列表列表,并将其显示在控制台上。
使用列表解析来迭代列表中的元素,并将唯一元素的长度与列表中每个元素的长度进行比较。
如果它们相等,则将它们存储在列表中并分配给一个变量。
这将作为输出显示在控制台上。
广告