V
V
vagitaku2019-05-22 12:53:15
Python
vagitaku, 2019-05-22 12:53:15

How to loop through two arrays and find matches (and then change the color of the word)?

There are two arrays of words (left_text and right_text). It is necessary to sort out the first for matches in the second. And if there is a match, perform certain actions.

def compare():
    if win.left_textBrowser.toPlainText() != '' and win.right_textBrowser.toPlainText() != '':
        for x in left_text:
            print(str(x) + ' check')
            if x in right_text:
                print(str(x) + ' ok')
                win.left_textBrowser.setText(win.left_textBrowser.toPlainText() + green_text + str(x) + green_text_end)
            else:
                print(str(x) + ' no')
                win.right_textBrowser.setText(win.right_textBrowser.toPlainText() + str(x))

But the print I see is this:

...массив 'Были', 'подобранны', 'оптимальные', 'технологи', 'и', 'решения', 'для', 'данной', 'задачи.'] check
...массив 'Были', 'подобранны', 'оптимальные', 'технологи', 'и', 'решения', 'для', 'данной', 'задачи.'] ok

What am I doing wrong? In for, I seem to correctly take each word from the list, and in if I check for a match of this word in right_text. Further, if there are matches, green_text should be inserted before inserting the word and after green _text_end.
green_text = '<span style=\" font-size:8pt; font-weight:600; color:#00ff00;\" >'
green_text_end = '</span>'

But even though I load the same texts, the color does not change. I decided to see what was the matter, using print, and here it is. Something is wrong with the array sorting.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kova1ev, 2019-05-22
@kova1ev

did not quite understand what you are doing, but a list of matching elements of two lists can be found like this:
then, perhaps like this:

for x in result:
    win.left_textBrowser.setText(win.left_textBrowser.toPlainText() + green_text + str(x) + green_text_end)

R
Ruslan., 2019-05-22
@LaRN

Try to do print(left_text) maybe your left_text is not a list of strings, but a list with a nested list of strings.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question