I'm not asking about the actual time to calculation.

In a nested for loop, you can count the number of times you step into the innermost for loop, and measure efficiency that way.

Can this be done with a list comprehension?

My actual code may or may not be helpful.

Nested For-loop : --------------------------------

    scanWindowSize = len(B) - 1
    mylist = []
    loopsteps = 0
    
    for scanWindowSize in range(len(B),0,-1):
        for i in range(len(B) - scanWindowSize + 1):
            if B[i:i + scanWindowSize] in C:
                mylist.append([B[i:i + scanWindowSize]])
    
    
                loopsteps += 1
    
    if len(mylist) > 0:
        break

List comprehension nested within a For-loop: -----------------

    for scanWindowSize in range(len(B),0,-1):
        mylist = [B[i:i + scanWindowSize] for i in range(len(B) - scanWindowSize + 1) if B[i:i + scanWindowSize] in C]
        if mylist:
            break
  • This code compares substrings of string B to string C to find the largest string (or strings) that appear in each string, and returns a list of that string or string(s)

outer for loop executes once for each size of substring of B (unless we hit break) for each character position in the current substring, take the substring beginning at that character position and of the current scanWindowSize, and append it to mylist if it appears in C increment loopsteps by 1 to count the number of times we enter the inner for loop*

0

There are 0 best solutions below