Skip to content
Advertisement

python how to record the number of elements in listA that appear in the same order as in listB

I am trying to trace to what extent is listA, listB, listC… similar to the original list. How do I print the number of elements that occur in the same sequence in listA as they occur in the original list?

JavaScript

Advertisement

Answer

I wrote a function that does the job I think. It might be a bit too complex, but I can’t see an easier way at the moment:

JavaScript

If you call it with the original list and example lists, it prints the following.
get_sequence_lengths(original, listA) prints [2] [['my', 'dog']].
get_sequence_lengths(original, listB) prints [5] [['I', 'live', 'in', 'space', 'with']].
get_sequence_lengths(original, listC) prints [2, 4, 2] [['I', 'live'], ['in', 'space', 'with', 'my'], ['my', 'dog']].

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement