示例如下:
zx = [8, 3, 12, 1, 2, 34, 54, 12, 34, 11, 12, 14, 9, 1, 10, 15, 17, 18, 99, 1, 3, 5, 10]
duplicate_nums = []
for i in zx:
i_show_count = zx.count(i)#每个值 出现了几次
if i_show_count > 1 and [i,i_show_count]not in duplicate_nums:
duplicate_nums.append([i,i_show_count])
print(duplicate_nums)
for item in duplicate_nums:
# duplicate_n = item[0]
# duplicate_times = item[1]
duplicate_n,duplicate_times=item
for j in range(duplicate_times-1):#为何减一,因为是去重,不是全部删除,所以得保留一个
zx.remove(duplicate_n)#一次只能删除一个值
print("删除了一次:",duplicate_n)
print(zx)
运行结果如下: