0

I have almost completed a script to compare two lists (listA and listB). The script should return the similar values between the lists, that are following each others, and with single occurrences.

listA=['b', 'c', 'd', 'c', 'c']
listB=['a', 'b', 'c']

    def ordered_matches(list1, list2):
    
    list = []
    
    len1 = len(list1) 
    len2 = len(list2)
    
        start_range = 0
        for f1 in range(len1):
            for f2 in range(start_range, len2):
    
                if list1[f1] == list2[f2] and f2 != len2:      
                    list.append(list1[f1]);
                    start_range = f2;
                    break
                elif list1[f1] == list2[f2] and f2 == len2:
                    list.append(list1[f1]);
                    continue
                    break
        return list
    
    ordered_matches(listA, listB)

Here are examples of input/output expected:

listA=['a', 'b', 'c', 'd', 'e']
listB=['d', 'e', 'f', 'g', 'h']
output=['d','e']

listA=['a', 'b', 'c', 'd', 'e']
listB=['h', 'd', 'g', 'd', 'f']
output=['d']

listA=['b', 'a', 'c', 'a', 'e']
listB=['b', 'a', 'e', 'b', 'e']
output=['b', 'a', 'e']

To reach this result, the first for loop of the script should be broken. But unfortunately, I can't manage to break the first for loop within the second for loop.

Would you have any advice to do it?

  • 2
    Does this answer your question? [How to break out of multiple loops?](https://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops) – TheFungusAmongUs May 11 '22 at 13:44
  • Note: For the above link, [the second highest upvoted answer](https://stackoverflow.com/a/3150107/16177247) is the more pythonic way of writing this. – TheFungusAmongUs May 11 '22 at 13:45
  • As additional info: A labeled break was rejected here https://peps.python.org/pep-3136/ and so that question seems like it is still the currently up-to-date information. – kutschkem May 11 '22 at 13:47
  • @TheFungusAmongUs thank for your answer, unfortunately this answer does not work. If I design my elif statement with the break statement at the same level than elif, the loop does not work. – Drosera_capensis May 11 '22 at 13:53
  • @Drosera_capensis you can still break from within the `if/elif` statements – TheFungusAmongUs May 11 '22 at 14:06
  • @TheFungusAmongUs It seems that the if and elif loops here can break the inner loop. But I can't find a way to break the outer loop. My apologizes oda, I don't understand the question. You ask if I can use modules? – Drosera_capensis May 11 '22 at 14:10
  • @Drosera_capensis yes, once you break from the inner loop, you can use the method in the linked post to break the outer loop – TheFungusAmongUs May 11 '22 at 14:12
  • @TheFungusAmongUs As I mentionned earlier, this method does not work. I have tried the with the negative indent (break at level with elif), and the output is not the one expected. – Drosera_capensis May 11 '22 at 14:15
  • @oda, if you can recomand any tool to get ordered single matches between lists, I would be glad. – Drosera_capensis May 11 '22 at 14:16
  • @Drosera_capensis Could you give a few more examples of input plus expected output? I want to be on the same page with you when you say that the "script should return the similar values between the lists, that are following each others, and with single occurrences". – OTheDev May 11 '22 at 15:14
  • 1
    Apologizes if the outputs wanted were unclear. I search for matches from listA to listB, respectful of the order of the list. `listA=['a', 'b', 'c', 'd', 'e']` `listB=['d', 'e', 'f', 'g', 'h']` `output=['d','e']` or `listA=['a', 'b', 'c', 'd', 'e']` `listB=['h', 'e', 'g', 'd', 'f']` `output=['d']` or `listA=['a', 'b', 'c', 'd', 'e']` `listB=['b', 'a', 'e', 'b', 'e']` `output=['a', 'b', 'e']` – Drosera_capensis May 11 '22 at 15:36
  • Ah okay. Thanks for providing more information. I think I understand now. I won't be able to try to code up a solution today as I have something to attend to but I have a feeling you should get a working solution by tomorrow (either from your own efforts and/or some answer). I will still try to check back tomorrow. – OTheDev May 11 '22 at 16:23
  • 1
    May I advise that you edit your post to include the sample input/output you just gave me? I would also advise that you edit your post to include the information you provided in your answer (and delete the answer) as answer posts should only be for answers (do of course provide your answer if you find a solution). – OTheDev May 11 '22 at 16:25
  • 1
    @oda Thank you very much for the help. I keep you updated on what I can find on my side. – Drosera_capensis May 11 '22 at 16:25

1 Answers1

1

I think I have found an answer that does not require to break the outer loop. The main question of this topic is not addressed, but at least the script seems to work.

def ordered_matches(list1, list2):

    list = []

    len1 = len(list1)
    len2 = len(list2)

    start_range = 0
    for f1 in range(len1):
            for f2 in range(start_range, len2):

                if list1[f1] == list2[f2]:      
                    list.append(list1[f1]);
                    start_range = f2+1;
                    break
    return list

However, it is noticeable that the position of the inputs in the command can change the outputs.