欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

几种查重算法(python版)--查找、删除数组中重复元素的算法

程序员文章站 2022-06-11 16:05:36
...

几种查找数组中重复元素的算法(python版)

最简单粗暴的在4

1、利用set方法和count(list.obj) 实现删除重复和统计重复。

代码如下:

# set集合方法去重   count(list.obj)记录出现次数

str_1 = r'''It was designed by the prestigious architect Zaha Hadid and has a wave-like roof that is 160 metres long.
Today's special events are designed to arouse interest in the Olympics around the world and to encourage British fans too.
Many failed to get Olympic tickets in the recent sales process.
According to a new survey for the BBC, 53% of Londoners think the process was "not fair".
But the same survey found support is growing for London 2012. Of the 1,000 people surveyed, 73% said they backed the Games - up from 69% in 2006'''

new_list = list(str_1.split(' '))
new_set = set(new_list)
for i in new_set:
    print(str(i)+"一共出现了"+str(new_list.count(i))+"次!")

2、通过排序后得到有序数组,再类似于插入排序一样两两比较

代码如下:

# 先排序 比较前后两个值大小
# 注意,remove删除后后面的元素会补上来,i值会变,len值也会变,所以会出现index值超出范围的错误
number_list = [2,4,6,3,7,4,9,10]
number_list.sort()
print(number_list)

# 第一种
# for i in range(1,len(number_list)):
#     while i < len(number_list) and number_list[i] == number_list[i-1]:
#         number_list.remove(number_list[i])

# print(number_list)

# 第二种
# 从后往前,索引是负数,防止索引越界
for i in range(len(number_list)-2,-1,-1):
	# print(i)
	if number_list[i+1] == number_list[i]:
		# del lists[i]
		number_list.remove(number_list[i])

print(number_list)

3、用第三方模块numpy

代码如下:

# 第三方库  numpy 的 unique 函数
import numpy as np
lists = [1,1,2,3,4,6,9,6,2,2]
lists = np.unique(lists)
print(lists)

4、循环

# 最简单粗暴的循环
lists = [1,5,6,7,1,5,6,7,9]
new_list = []
for i in lists:
    if i not in new_list:
        new_list.append(i)
    else:
        print(str(i)+"重复了"+str(lists.count(i))+"次")

print(new_list)