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

几种查找数组中重复元素的算法(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)
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值