title: Multiple loops logic and speed optimization in Python?
body: <p>Here is two python functions to transfer data from one file to another file. Both source file and target file have the same number of objects but with different data.  </p>
  
  <pre><code>def getBlock(rigObj, objName):
      rigObj.seek(0)
      Tag = False
      block = ""
      for line in rigObj:
          if line.find("ObjectAlias " + str(objName) + "\n") != -1:
              for line in rigObj:
                  if line.find("BeginKeyframe") != -1:
                      Tag = True
                  elif line.lstrip().startswith("0.000 ") and line.rstrip().endswith("default"):
                      Tag = False
                      break
                  elif Tag:
                      block += line
      return (block)
  
  def buildScene(sceneObj, rigObj, objList):
      sceneObj.seek(0)
      rigObj.seek(0)
      newscene = ""
      for line in sceneObj:
          newscene += line
          for obj in objList:
              if line.find("ObjectAlias " + obj + "\n") != -1:
                  Tag = True
                  for line in sceneObj:
                      if line.find("BeginKeyframe") != -1:
                          newscene += line
                          newscene += getBlock(rigObj, obj)
                          Tag = False
                      elif line.lstrip().startswith("0.000 ") and line.rstrip().endswith("default"):
                          newscene += line
                          Tag = True
                          break
                      elif Tag:
                          newscene += line
      return (newscene)
  </code></pre>
  
  <p><strong>getBlock</strong> is a sub-function for getting data from rigobj;<br>
  <strong>buildScene</strong> is my main function, it has three parameters:<br>
  First parameter(sceneobj) is the file that I want to put data into;<br>
  Second parameter(rigobj) is the file that I get the data from;<br>
  Third parameter(objlist) is a list of what object's data to be transfered.  </p>
  
  <p>So far, the function does its job, the only problem is a bit of slow(sceneobj&lt;10MB, rigobj&lt;2MB, objlist&lt;10 objects), I am not sure if there are a logic problem in the code, should I loop the sceneObj first or loop the objList first? Does it affect the speed?</p>
  
  <p><strong>UPDATE:</strong><br>
  Both sceneObj and rigObj have similar data like this:</p>
  
  <pre><code>lines
  BeginObject               
  lines                     
  ObjectAlias xxx           #--&gt; object in transfer list
  lines                     
  BeginKeyframe 10 12       
    -9.000 4095 default     #--&gt; transfer begins
    lines                   #--&gt; transfer from rigObj to sceneObj and override lines in sceneObj
    -8.000 63 default       #--&gt; same
    lines                   #--&gt; same
    -7.000 63 default       #--&gt; same
    lines                   #--&gt; same
    -1.000 63 default       #--&gt; same
    lines                   #--&gt; transfer ends
    0.000 -1 default        
    lines                   
  EndKeyframe               
  EndMotion                 
  lines                     
  EndObject
  </code></pre>
  
  <p>The data want to be transfered and overrided is only lines bewteen <strong>BeginKeyframe</strong> and <strong>0.000 -1 default</strong> of any specified objects(by objList) </p>