Hi, i'm trying to take the last list from a list of lists, use that as a start point, then iterate through the list of lists and find the closest match, then use that matched list as a base and iterate through the remaining lists for the closest match to that list,then use that list as a base, and so on. Reordering the original list of lists.
SequenceMatcher seems to do the job, to get the first closest match.
How could i change the code so it would do what is required from within a function, or indeed in any other way.
I am new to both Python and this forum, as you can probably tell from my code. Here it is.
List1=[[6,5,4],[2,3,5],[1,2,3],[3,4,5],[1,2,3,4]] print List1 List2= LastvaL=List1.pop() List2.append(LastvaL) from difflib import SequenceMatcher i=0 s= SequenceMatcher() s.set_seq2(LastvaL) s.set_seq1(List1[i]) hoLder= previous=0 print "previous",previous for item in List1[0:-1]: print item s.set_seq1(List1[i]) s.ratio() current=s.ratio() if current>previous: print "current",current,"previous",previous previous=current hoLder= hoLder.append(List1[i]) print "ratio",s.ratio() i=i+1 List2.append(hoLder) print List2
I have included an example list in this code.
I would like to put this code or similar in a function because my list has a hundred lists in it, but I dont know how to do that.
Any help or ideas would be greatly appreciated. Thank you.