需要从列表中删除重复项。 Set()函数不起作用。 for循环方法也不是

我正在使用xlrd从Excel表格中提取数据。 我想要的数据是在两列(“ID”和“位置”列)。 每列包含成千上万的条目,其中大部分是完全重复的。 我只是试图创build2个列表,其中包含来自两个Excel列的所有唯一条目。 这是我的大部分代码,并显示了打印其中一个列表时显示的内容的示例:

rawIDs = data.col_slice(colx=0, start_rowx=0, end_rowx=None) #getting all of column 1 in a list IDs = [] for ID in rawIDs: if ID not in IDs: IDs.append(ID) #trying to create new list without duplicates, but it fails rawlocations = data.col_slice(colx=1, start_rowx=0, end_rowx=None) #getting all of column 2 in a list locations = [] for location in rawlocations: if location not in locations: locations.append(location) #same as before, also fails print set(IDs) #even set() doesn't remove duplicates, it just prints "rawIDs" 

不pipe我怎么做,它总是打印原始列表,剩下所有的重复。

不言而喻,我已经看了很多其他类似的stackoverflowpost,他们的解决scheme不适合我。

编辑:我错了一个特定的。 我意识到印刷

 print set(IDs) 

实际上返回

“set([item,item,item …])”作为输出。 所以它基本上把“set()”放在“rawIDs”输出的周围。 这对我来说没有任何意义,虽然…

另外这里是一个示例屏幕截图:

这里是一个示例截图

解决scheme:

看起来元数据(比如表格中的坐标位置)被存储起来,因此,即使文本可能相同,由于这个元数据,列表中的每个项目都是不同的。

修改for循环,使它们添加项目的string ,而不是项目本身,解决了我的问题,并取得了新的列表没有重复。

 rawIDs = data.col_slice(colx=0, start_rowx=5000, end_rowx=5050) IDs = [] for ID in rawIDs: if str(ID) not in IDs: IDs.append(str(ID)) rawlocations = data.col_slice(colx=1, start_rowx=0, end_rowx=None) locations = [] for location in rawlocations: if str(location) not in locations: locations.append(str(location)) print IDs #it prints a list with no duplicates!