【问题标题】:very slow loop in python 3, tkinterpython 3中的非常慢的循环,tkinter
【发布时间】:2020-09-23 10:27:40
【问题描述】:

我正在编写一些带有界面的脚本,但遇到了一些性能非常差的问题。

我想每列创建多个条目(129),列应该是 7。为此我创建了一个字典,因此创建了具有动态变量名称的空条目。

对于这些空条目,我想根据特定单元格(国家/地区)中提供的内容插入文本。

任务完成了,但是,加载数据需要很长时间,我不知道可以做些什么来让它更快..我不能让它这么慢,尤其是在这个阶段那个项目。

从提取文件中获取信息的功能:

def Load_Neighborhood_Details(self, inx, what_return):
    file_path = r'C:\Users\krzysztof-wirkus\Desktop\Od nowa\extracts\neighborhood_details.csv'
    file = pd.read_csv(file_path, encoding = "ISO-8859-1")
    
    country = file[file['Country Name'] == self.p0.country_input.get()]
    location = country[country['Location Name'] == self.p0.location_input.get()].iloc[inx]
    
    neighborhood_name = location['Neighborhood Name']
    dwelling = location['Dwelling']
    
    furniture = location['Furnished Unfurnished Indicator']
    item_cost_category = location['Item Cost Category Name']
    neigh_class = location['Class']
    try:
        start_date = location['Start Date'].split()[0]
    except:
        start_date = ""
    try:
        end_date = location['End Date'].split()[0]
    except:
        end_date = ""
    if what_return == "neighborhood_name":
        return neighborhood_name
    elif what_return == "dwelling":
        return dwelling
        
    elif what_return == "furniture":
        return furniture
    elif what_return == "item_cost_category":
        return item_cost_category
    elif what_return == "neigh_class":
        return neigh_class
    elif what_return == "start_date":
        return start_date
    elif what_return == "end_date":
        return end_date

我糟糕的表现循环:

for i in range(2, 131):
        self.p3.dict['neighborhood_details_name_entry_' + str(i)].insert(tk.END, self.Load_Neighborhood_Details(i-2, "neighborhood_name"))
        self.p3.dict['neighborhood_details_dwelling_entry_' + str(i)].insert(tk.END, self.Load_Neighborhood_Details(i-2, "dwelling"))
        self.p3.dict['neighborhood_details_furniture_entry_' + str(i)].insert(tk.END, self.Load_Neighborhood_Details(i-2, "furniture"))
        self.p3.dict['neighborhood_details_item_cost_category_entry_' + str(i)].insert(tk.END, self.Load_Neighborhood_Details(i-2, "item_cost_category"))
        self.p3.dict['neighborhood_details_class_entry_' + str(i)].insert(tk.END, self.Load_Neighborhood_Details(i-2, "neigh_class"))
        self.p3.dict['neighborhood_details_start_date_entry_' + str(i)].insert(tk.END, self.Load_Neighborhood_Details(i-2, "start_date"))
        self.p3.dict['neighborhood_details_end_date_entry_' + str(i)].insert(tk.END, self.Load_Neighborhood_Details(i-2, "end_date"))

【问题讨论】:

    标签: python performance loops tkinter


    【解决方案1】:

    我不确定,但问题可能是您继续打开 csv 文件。 尝试改变

    def Load_Neighborhood_Details(self, inx, what_return):
        file_path = r'path/to/file'
        file = pd.read_csv(file_path, encoding = "ISO-8859-1")
        
        country = file[file['Country Name'] == self.p0.country_input.get()]
        [...]
    

    与:

    def Load_Neighborhood_Details(self, inx, what_return, file):
        country = file[file['Country Name'] == self.p0.country_input.get()]
        [...]
    

    然后:

    file_path = 'path/to/file'
    file = pd.read_csv(file_path, encoding = "ISO-8859-1")
    for i in range(2, 131):
        self.p3.dict['neighborhood_details_name_entry_' + str(i)].insert(tk.END, self.Load_Neighborhood_Details(i-2, "neighborhood_name", file))
        [...]
    
    

    (注意传递给函数Load_Neighborhood_Details()的文件对象)

    希望对你有帮助!

    【讨论】:

    • 由于它们都是方法,最好将file 存储在对象上(如self.file)而不是传递它。
    • 是的,我同意。这只是一个例子,因为在问题中没有定义类^^
    【解决方案2】:

    您的代码性能很差,因为您多次打开 csv 文件,您应该只打开一次(如果可能)并作为函数的参数传递,这将有助于提高性能。

    【讨论】:

      猜你喜欢
      • 2016-09-07
      • 1970-01-01
      • 2022-01-22
      • 2012-09-20
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多