【发布时间】:2020-02-18 23:05:50
【问题描述】:
晚上好。我有一个包含邮政编码和相关信息的 excel 文件。这些邮政编码有很多重复。我想通过将它们全部放在一个没有重复的列表中来弄清楚我有哪些邮政编码。这段代码可以运行,但运行速度很慢(花了 100 多秒),我想知道我能做些什么来提高它的效率。
我知道每次都必须检查整个列表是否有重复项会大大降低效率,但我不知道如何解决这个问题。我也知道遍历每一行可能不是最好的答案,但我还是很新,现在卡住了。
提前致谢。
import sys
import xlrd
loc = ("locationOfFile")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
def findUniqueZips():
zipsInSheet = []
for i in range(sheet.nrows):
if str(sheet.cell(i,0).value) in zipsInSheet:
pass
else:
zipsInSheet.append(str(sheet.cell(i,0).value))
print(zipsInSheet)
findUniqueZips()
【问题讨论】:
标签: python performance xlrd zipcode