深度阅读

How to remove duplicates from a Python list or array?

作者
作者
2023年08月22日
更新时间
9.02 分钟
阅读时间
0
阅读量

To remove duplicates from a Python list or array, you can use the set() function on the list. Here is an example:

my_list = [1, 2, 3, 2, 4, 3]
unique_list = list(set(my_list))
print(unique_list)

This code will output [1, 2, 3, 4], which is the original list with duplicates removed.

If you want to preserve the original order of the list, you can use a combination of set() and a list comprehension:

my_list = [1, 2, 3, 2, 4, 3]
unique_list = []
for item in my_list:
  if item not in unique_list:
    unique_list.append(item)
print(unique_list)

This code will also output [1, 2, 3, 4], but the order of the items will be preserved.

If you’re working with a NumPy array, you can use the np.unique() method:

import numpy as np
my_array = np.array([1, 2, 3, 2, 4, 3])
unique_array = np.unique(my_array)
print(unique_array)

This code will also output [1, 2, 3, 4].

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。