python3之list去重

今天想编程写一个获取豆瓣房源信息的脚本,有的用户重复发表帖子,但内容一致的,因此需要根据用户名去除重复信息,所以要使用到列表去重。


list2 = []  #用于保存不重复元素的列表
list1 = [1,2,3,2,2,2,4,6,5]  #待去重的列表

for i in range(len(list1)):
    if list1[i] not in list2:
        list2.append(list1[i])  #将不重复元素储存在list2中
    else:
        print(i,list1[i]) #输出重复元素及其下标
        
print(list2)

3 2
4 2
5 2
[1, 2, 3, 4, 6, 5]

发布了31 篇原创文章 · 获赞 2 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_27149279/article/details/105385706