ArrayMatched

import os

from jinja2 import Environment,FileSystemLoader



def generateNewLackArray(ArrayList,count,TargetArray):
    ArrayList=list(ArrayList)
    newArrayList=[]
    matchResult=[]
    # i=0
    for i in range(0,ArrayList.__len__()-count+1):
        LackArray = ArrayList
        if ArrayList[i]=="N":
            continue

        CurrentValue=LackArray[i]
        for tube in range(i+1,ArrayList.__len__()-count+2):
            TotalValue=CurrentValue
            Process=str(i+1)
            newArray={}
            for c in range(0,count-1):

                index=tube+c
                if LackArray[index]=="N":
                    continue

                TotalValue=TotalValue+LackArray[index]
                Process=Process+"+"+str(index+1)

            newArray[str(TotalValue)]=Process
            if TotalValue in TargetArray:

                result={}
                result[Process]=list(TargetArray).index(TotalValue)+1
                matchResult.append(result)
                TargetArray[(list(TargetArray).index(TotalValue))]="N"

                popArray=Process.split("+")
                popArray.reverse()
                for p in popArray:
                    ArrayList[(int(p)-1)]="N"
                break


        # i+=1


    # print(ArrayList)
    # print(TargetArray)
    return matchResult,ArrayList,TargetArray


def getArrayList(path):
    if not os.path.exists(path):
        return []
    with open(path,"r") as reader:
        ArrayList=reader.read().split("
")
        for i in range(0,ArrayList.__len__()):
            if ArrayList[i]=="":
                ArrayList.pop(i)
        return ArrayList

def moretimes(ArrayList,TargetList):
    ArrayList=list(ArrayList)
    TargetList=list(TargetList)
    Result=[]
    for count in range(2,ArrayList.__len__()-1):
        matchResult,ArrayList,TargetList= generateNewLackArray(ArrayList,count,TargetList)
        Result=Result+matchResult
    return Result


def generateHTML(InputArray,Target,Result):
    env=Environment(loader=FileSystemLoader("./"))
    template=env.get_template("template.html")
    with open("result.html","w+") as f:
        html_content=template.render(InputArray=InputArray,Target=Target,Result=Result)
        f.write(html_content)


if __name__=="__main__":
    a=[8,1,2,3,4,1,2,3,4,1,2,1,1,1,1,1,1,1]
    b=[1,3,7,6,7]
    a=getArrayList("1.txt")
    a=getArrayList("2.txt")
    generateHTML(a,b,moretimes(a,b))



<html>
<body>

<table border="1" width = "10%" cellspacing='0' cellpadding='0' align='left'>
    <tr>        
        <th>序号</th>
    </tr>
   {% for id in range(1,InputArray.__len__()+1) %}
    <tr align='center'>
      <td>{{ id }}</td>
      </tr>
       {% endfor%}
    </table>
    
    <table border="1" width = "20%" cellspacing='0' cellpadding='0' align='left'>
    <tr>        
        <th></th>
    </tr>
       {% for item in InputArray %}
       <tr align='center'>
       <td>{{ item }}</td>
    </tr>
     {% endfor%}
    </table>

<table border="1" width = "30%" cellspacing='0' cellpadding='0' align='left'>
    <tr>        
        <th>公式</th>
        <th>结果</th>
       
    </tr>
 
    {% for item in Result %}
    <tr align='center'>
    {% for k,v in item.items()%}
        <td>{{ k }}</td>
        <td>{{ v }}</td>
         {% endfor%}
    </tr>
    {% endfor%}
    </table>

<table border="1" width = "10%" cellspacing='0' cellpadding='0' align='left'>
    <tr>        
        <th>序号</th>
    </tr>
   {% for id in range(1,Target.__len__()+1) %}
    <tr align='center'>
      <td>{{ id }}</td>
      </tr>
       {% endfor%}
    </table>
    
    <table border="1" width = "20%" cellspacing='0' cellpadding='0' align='left'>
    <tr>        
        <th></th>
    </tr>
       {% for item in Target %}
       <tr align='center'>
       <td>{{ item }}</td>
    </tr>
     {% endfor%}
    </table>
</body>
</html>


 
原文地址:https://www.cnblogs.com/Zeech-Lee/p/11698448.html