Search code examples
pythonpython-3.xfor-looptkinternested-loops

How to get all the values of 2nd loop output into the 3rd loop end_idx variable?


This question may have been already answered but after much time searching SO and the net I did not find a suitable answer.

I have 3 consecutive loops as follows:

    w = my_text0.get(1.0, END).split()

    data = []
    # get & set the matching dict keys and values
    for i in w:
        if i in [*dictionary]:
            word = i.title()
            my_text.insert(END, word)
            my_text.insert(END, ", " + dictionary[i] + "\n")

            keys_len = len(word)
            print("keys_len", keys_len)
            data.append(keys_len)

    print("data", data)

    # print("keys_len", keys_len)
    # get text widget lines count
    txtwidg_lnescnt = int(my_text.index("end-1c").split(".")[0])

    for j in range(0, len(data)):
        k_l = data[j]
        print("k_l", k_l)
    print("k_l out", k_l)

    # get index start and end for tag_add method
    for k in range(1, txtwidg_lnescnt):
        lne_idx = k
        str_idx = f"{lne_idx}.0"
        end_idx = f"{lne_idx}.{k_l}"
        print("end_idx", end_idx)
        my_text.tag_add("start", str_idx, end_idx)
        my_text.tag_config(
            "start",
            background="red",
            foreground="black",
            font=bdfont,
        )

I need the k_l values from the 2nd loop to be processed in the 3rd loop. But currently only the last value (9) from the 2nd loop is processed.

Here's the output I'm getting:

keys_len 5
keys_len 6
keys_len 9
data [5, 6, 9]
k_l 5
k_l 6
k_l 9
k_l out 9
end_idx 1.9
end_idx 2.9
end_idx 3.9

I need it as this:

keys_len 5
keys_len 6
keys_len 9
data [5, 6, 9]
k_l 5
k_l 6
k_l 9
k_l out 9
end_idx 1.5
end_idx 2.6
end_idx 3.9

I'm familiar with the append to an empty list method to store all the values from a loop.

But I can't repeat it here as it was already done in the 1st loop.

I also tried using the return from a separate function method but it outputs as in a nested loop and that's not what I need:

def mykeyslengths():
    # get the user's entered words
    w = my_text0.get(1.0, END).split()

    data = []
    # get & set the matching dict keys and values
    for i in w:
        word = i

        keys_len = len(word)
        print("keys_lenzz", keys_len)
        data.append(keys_len)

    for j in range(0, len(data)):
        k_l = data[j]
        print("k_l", k_l)

    return k_l


def generate():
    # Clear The text
    my_text.delete(1.0, END)

    # get the user's entered words
    w = my_text0.get(1.0, END).split()

    # data = []
    # get & set the matching dict keys and values
    for i in w:
        if i in [*dictionary]:
            word = i.title()
            my_text.insert(END, word)
            my_text.insert(END, ", " + dictionary[i] + "\n")

    #         keys_len = len(word)
    #         print("keys_len", keys_len)
    #         data.append(keys_len)

    # print("data", data)

    # print("keys_len", keys_len)
    # get text widget lines count
    txtwidg_lnescnt = int(my_text.index("end-1c").split(".")[0])

    # for j in range(0, len(data)):
    #     k_l = data[j]
    #     print("k_l", k_l)
    # print("k_l out", k_l)

    # get index start and end for tag_add method
    for k in range(1, txtwidg_lnescnt):
        lne_idx = k
        str_idx = f"{lne_idx}.0"
        end_idx = f"{lne_idx}.{mykeyslengths()}"
        print("end_idx", end_idx)
        my_text.tag_add("start", str_idx, end_idx)
        my_text.tag_config(
            "start",
            background="red",
            foreground="black",
            font=bdfont,
        )

Which output as this:

keys_lenzz 5
keys_lenzz 6
keys_lenzz 9
k_l 5
k_l 6
k_l 9
end_idx 1.9
keys_lenzz 5
keys_lenzz 6
keys_lenzz 9
k_l 5
k_l 6
k_l 9
end_idx 2.9
keys_lenzz 5
keys_lenzz 6
keys_lenzz 9
k_l 5
k_l 6
k_l 9
end_idx 3.9

If that was not clear from the examples, I need the lengths 5, 6, 9 to bold and modify the backgrounds and foregrounds of the respective substrings lengths determined by the end_idx variable's values with a tkinter tag_add end index property.

The problem I get currently is that all 3 substrings (the dictionary keys) are the same lengths (9) whereas they should all be distinct as per their respective lengths (5, 6, 9).


Solution

  • This should work:

        w = my_text0.get(1.0, END).split()
    
    data = []
    # get & set the matching dict keys and values
    for i in w:
        if i in [*dictionary]:
            word = i.title()
            my_text.insert(END, word)
            my_text.insert(END, ", " + dictionary[i] + "\n")
    
            keys_len = len(word)
            print("keys_len", keys_len)
            data.append(keys_len)
    
    print("data", data)
    
    # print("keys_len", keys_len)
    # get text widget lines count
    txtwidg_lnescnt = int(my_text.index("end-1c").split(".")[0])
    
    for j in range(0, len(data)):
        k_l = data[j]
        print("k_l", k_l)
    print("k_l out", k_l)
    
    # get index start and end for tag_add method
    for k in range(1, txtwidg_lnescnt):
        lne_idx = k
        str_idx = f"{lne_idx}.0"
        end_idx = f"{lne_idx}.{data[k-1]}"
        print("end_idx", end_idx)
        my_text.tag_add("start", str_idx, end_idx)
        my_text.tag_config(
            "start",
            background="red",
            foreground="black",
            font=bdfont,
        )