将城市和排序与文件进行比较的代码
for city in uniqueCity:
file = open(city+".txt","a+")
for data in salesData:
if data[2] == city:
file.write(",".join(data).replace(","," "))
file.write("\n")
file.close()
答案 0 :(得分:1)
该功能很慢,因为算法很慢。
正如所写,对于每个city
(循环len(uniqueCity)
次),它必须遍历所有salesData
(len(salesData)
次),因此总比较数执行的是len(uniqueCity) * len(salesData)
。在这种情况下(我猜是city
是str
),你可以做得更好,因为字符串是可以删除的。
groupedSalesData = {city:[] for city in uniqueCity}
for data in salesData:
city = data[2]
if city in groupedSalesData:
groupedSalesData[city].append(data)
for city, dataEntries in groupedSalesData.items():
file = open(city + ".txt", "a+")
for data in dataEntries:
file.write(",".join(data).replace(","," "))
file.write("\n")
file.close()
正如您所看到的,此算法的复杂性仅为len(uniqueCity) + len(salesData)
(假设data
需要O(1)
次复制,并且因为Python dict
上的操作应为{ {1}}),这要好得多。