Yeah, as I said before this code was written to work with the exact example given. That said, the easiest way I can think of to make it scalable is to use another txt file. Maybe even make it a full config file, whatever meets your requirements better. But basically, all the possible first three letter combos will be put into the file in the correct order. E.g.
These will the be read and put into a dictionary. All those if statements then get reduced and rewritten as they weren’t the best anyway. The new way to work out if it should have a gap put in is to store a next expected position variable. I.e. if you just appended 1 the next one should be 2, if it isn’t then all that it will do is do the current position (say 4) and minus the expected position and time the result by the gap (plus a space inbetween). All that needs to really be handled then is:
- “abc” coming up early and printing the current line starting a new one
- the end of a line due to finding the last element. (although this might just be able to be included in the above.
This way whenever you need to add a new three letter combo you add it to the reference text file and it should deal with it fine.
Dictionary will be something like:
with open("txtfile.txt", "r") as refText:
positions = dict()
for i, line in enumerate(refText):
positions[line[:3]] = i #shouldn't need the slice, however it will prevent issues with accidental spaces etc being added.
Don’t have time to put something together at the moment but should do tomorrow evening and if not then Saturday. But the above logic should do it.
The reason for the abc, abc ,abc missing a line was this bit below: As I said in the post above I thought it was wrong.
newLine = 
print >>outFile,line # Oops, this bit is wrong
newLine = 
newLine = [line] #Should have been
appendCount = 1 # Like this