Python循环优化与两个句子列表比较

问题描述 投票:0回答:1

我有一些python代码正常工作,但需要一段时间才能运行。我很确定它可以通过重新安排for循环或使用函数来优化,但我的尝试产生了不可思议的结果。

项目信息:

python版本= Iron Python 2.7(Revit Dynamo)

listMaster =所有可用句子的主列表

List1 =要与listMaster进行比较的第二个句子列表

List2 =与listMaster进行比较的第三个句子列表

当前工作流程:

  1. 循环通过List1
  2. 循环遍历listMaster中的每个项目
  3. 使用SequenceMatcher Ratio根据比率获得匹配
  4. 对List2,List3等重复上述操作 # Compare Excel Serious Warnings with All Revit Data for itemExcel in warnSeriousExcelData: for itemRvt in revitData: if SequenceMatcher(None, itemRvt, itemExcel).ratio() > 0.9: seriousLstCount = seriousLstCount + 1 srsData.append(itemExcel) # Compare Excel Troublesome Warnings with All Revit Data for itemExcel in warnTroublesomeExcelData: for itemRvt in revitData: if SequenceMatcher(None, itemRvt, itemExcel).ratio() > 0.9: troubleLstCount = troubleLstCount + 1 troubleData.append(itemExcel) # Compare Excel Bothersome Warnings with All Revit Data for itemExcel in warnBothersomeExcelData: for itemRvt in revitData: if SequenceMatcher(None, itemRvt, itemExcel).ratio() > 0.9: botherLstCount = botherLstCount + 1 botherData.append(itemExcel) # Compare Excel Benign Warnings with All Revit Data for itemExcel in warnBenignExcelData: for itemRvt in revitData: if SequenceMatcher(None, itemRvt, itemExcel).ratio() > 0.89: benignLstCount = benignLstCount + 1 benignData.append(itemExcel) # Compare Excel Unrecoverable Warnings with All Revit Data for itemExcel in warnUnrecoverExcelData: for itemRvt in revitData: if SequenceMatcher(None, itemRvt, itemExcel).ratio() > 0.8: unrecoverLstCount = unrecoverLstCount + 1 unrecoverData.append(itemExcel)

如果有帮助,请参见数据图像。 Data Image Example

我想也许可以加快迭代过程,也许可以翻转for循环。那么顶部循环将是整个listMaster,然后我会检查那个for循环中的每个List1,List2?但我不知道这是否是最快的方法?

对此的任何帮助都会很棒。我再次只有Python 2.7,所以我不能使用3.5等的一些新功能。

更新以运行减少循环长度但仍然运行缓慢。

for itemRvt in revitData:
    for itemExcel in warnSeriousExcelData:
        if SequenceMatcher(None, itemRvt, itemExcel).ratio() > 0.9:
                seriousLstCount = seriousLstCount + 1
                srsData.append(itemExcel)
    for itemExcel in warnTroublesomeExcelData:
        if SequenceMatcher(None, itemRvt, itemExcel).ratio() > 0.9:
                troubleLstCount = troubleLstCount + 1
                troubleData.append(itemExcel)
    for itemExcel in warnBothersomeExcelData:
        if SequenceMatcher(None, itemRvt, itemExcel).ratio() > 0.9:
                botherLstCount = botherLstCount + 1
                botherData.append(itemExcel)
    for itemExcel in warnBenignExcelData:
        if SequenceMatcher(None, itemRvt, itemExcel).ratio() > 0.89:
            benignLstCount = benignLstCount + 1
            benignData.append(itemExcel)
    for itemExcel in warnUnrecoverExcelData:
        if SequenceMatcher(None, itemRvt, itemExcel).ratio() > 0.8:
            unrecoverLstCount = unrecoverLstCount + 1
            unrecoverData.append(itemExcel)

马特

python python-2.7 performance for-loop revit
1个回答
0
投票

要有效地多次查找数据条目,不应使用列表来存储它们,而应使用字典或散列表。未排序的列表会强制您为每个查找迭代整个事物。字典或哈希表要快得多。数千倍的速度,有很多条目。将你的warnXyzExcelData系列转换为字典,一切都会好的。修复非常简单。祝好运并玩得开心点!

© www.soinside.com 2019 - 2024. All rights reserved.