Python-加快嵌套字典之间的迭代速度

时间:2020-01-08 23:09:19

标签: python performance loops dictionary openpyxl

我想问您一些建议,以加快我的代码的速度。我知道您会看到很多错误,但是我需要您的知识和帮助,问题出在哪里以及如何改进此代码。

背景-创建什么应用程序:

  1. 使用OpenPyXL
  2. 打开excel文件,读取数据并将其放入嵌套字典中: 2a。行的第一级 2b。 2级物品 示例:

{1:'@ 5C \ Qopen @',2:'20386239',3:'3000133215',4:'RA',5:无, 6:'Vendor2',7:'IM45',8:'@FR \ QNot due @',9:无,10:无,11: 'E1',12:'DNS',13:datetime.datetime(2019,12,27,0,0),14: datetime.datetime(2019,12,26,0,0),15:-21501,16:'GBP',17: -21501,18:'GBP',19:datetime.datetime(2019,12,26,0,0),20:datetime.datetime(2020,2,9,0,0)}

{2:'@ 5C \ Qopen @',2:'20386239', 3:'3000133215',4:'RA',5:无,6:'Vendor1',7:'IM45',8: '@FR \ QNot due @',9:无,10:无,11:'E1',12:'DNS',13: datetime.datetime(2019,12,27,0,0),14:datetime.datetime(2019,12, 26,0,0),15:-21501,16:'GBP',17:-21501,18:'GBP',19: datetime.datetime(2019,12,26,0,0),20:datetime.datetime(2020,2, 9,0,0)}

{3:'@ 5C \ Qopen @',2:'20386239', 3:'3000133215',4:'RA',5:无,6:'Vendor1',7:'IM45',8: '@FR \ QNot due @',9:无,10:无,11:'E1',12:'DNS',13: datetime.datetime(2019,12,27,0,0),14:datetime.datetime(2019,12, 26,0,0),15:-21501,16:'EUR',17:-21501,18:'GBP',19: datetime.datetime(2019,12,26,0,0),20:datetime.datetime(2020,2, 9,0,0)}

  1. 脚本应查看整个数据-比较供应商和货币,并查看特定供应商中我们是否使用不同的货币(例如,当供应商1 没有100%一种特定的货币[GBP或其他]
  2. 发生这种情况时-例如,将“东西”添加到不同货币所在行的“ 17”列中
  3. 主要是我的代码正常工作,非常慢。我的意思是,当我需要在同一时间文件中与 30000 行进行比较时。

您知道我该如何改进吗? 谢谢

next_row2 = 1
numerkolumny = 1
nastepny = 1
numer_vendora = 1
ilosc_gbp = 0
ilosc_inne = 0
linijkadanych = {}

lista_vendorow = {}

for zmienna2 in progressbar.progressbar(assets2, redirect_stdout=True):

    for iteracja in assets2:
        if assets2[zmienna2][6] not in lista_vendorow.values():

            if nastepny < len(assets2):
                if assets2[zmienna2][6] == assets2[nastepny+1][6]:
                    if assets2[nastepny+1][16] == "GBP": # JESLI ZNALAZLES GBP, POLICZ DO GBP
                        ilosc_gbp = ilosc_gbp + 1
                        nastepny = nastepny + 1
                    else:                               # JESLI ZNALAZLES INNA WALUTE, POLICZ DO INNEJWALUTY
                        ilosc_inne = ilosc_inne + 1
                        nastepny = nastepny + 1
                else:
                    nastepny = nastepny + 1

            if nastepny >= len(assets2): # JESLI PRZEITEROWALES PRZEZ WSZYSTKIE WIERSZE, OBLICZ WYNIK

                    suma_walut = ilosc_gbp + ilosc_inne # SUMUJ WSZYSTKIE WALUTY

                    # JESLI ZNAJDZIE ODCHYLELNIA - RAPORTUJ!
                    if (suma_walut != ilosc_gbp) and (suma_walut != ilosc_inne):

                        for waluty in assets2: # nr wiersza
                            for waluty2 in assets2[waluty]: # nr kolumny
                                if assets2[waluty][6] == assets2[zmienna2][6]:
                                    if ilosc_gbp > ilosc_inne:
                                        result_tab.cell(column=17, row=waluty+1, value="Waluta other than GBP. Check!").font = style_blad_bold
                                    else:
                                        result_tab.cell(column=17, row=waluty+1, value="Other currencies between GBP!. Check!").font = style_blad_bold

                    lista_vendorow[numer_vendora] = assets2[zmienna2][6]
                    ilosc_gbp = 0   # ZERUJ ZMIENNE, LICZYMY NOWEGO VENDORA
                    ilosc_inne = 0  # ZERUJ ZMIENNE, LICZYMY NOWEGO VENDORA
                    nastepny = 1    # ZERUJ ZMIENNE, LICZYMY NOWEGO VENDORA
                    numer_vendora = numer_vendora + 1

0 个答案:

没有答案