José Manuel Suárez Sierra wrote: > This is the traceback: > line 18, in <module> > for transf2[j] in transf2: > IndexError: list assignment index out of range > > If I have initialized j=0 (such as i) why does it not work?
A for loop for x in y: ... sequentually assigns every value in y to x. So with y = "ab" it executes x = "a" ... x = "b" ... This is the case even when there is something more complex like transf2[j]: for transf2[j] in y: ... translates to transf2[j] = "a" ... # both transf2 and j may be rebound here and thus affect transf2[j] = "b" # what this actually does ... and thus can fail with an IndexError when j is bound to a value greater than the length of the transf2 list. But most likely you want to avoid this feature of Python -- even the experts never use it. > I want the script to read sequence 1 and compares every element inside it > with elements in sequence 2 no mattering where it matches. That may be sufficient to explain the objective to a human who already has a rough idea of your goal, but to come up with an actual algorithm you need to think about -- and communicate -- a lot more detail. There is a third party module that operates on strings, not lists which finds the edit operations >>> Levenshtein.editops("abcdefgj", "axcdyezfg") [('replace', 1, 1), ('insert', 4, 4), ('insert', 5, 6), ('delete', 7, 9)] What should your function return given the equivalent lists ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'j'] ['a', 'x', 'c', 'd', 'y', 'e', 'z', 'f', 'g'] ? Try to make your plain-english description as precise as possible before you start coding. -- https://mail.python.org/mailman/listinfo/python-list