There is such a code.

name_elem_site = text_element_by_class_name(driver, "texttexttext") currentList = [] currentWord = currentList.append(str(name_elem_site)) if name_elem_site: for word in currentList: if word == currentWord: continue currentWord = word currentList.append(word) log.write("%s\n" % word) log.flush() 

Objective: to obtain data by word and write to the list, without recording a row of running data. Those. should be so

Word1, Word2, Word1, Word3, Word2, Word3, Word2, .., WordN

And I get

Word1, Word1, Word1, Word2, Word2, Word2, Word3, .., WordN

Tried to add if currentList[-1] != word

But the result is the same ...

 name_elem_site = text_element_by_class_name(driver, "texttexttext") currentList = [] currentWord = currentList.append(str(name_elem_site)) if name_elem_site: for word in currentList: if word == currentWord: continue currentWord = word if currentList[-1] != word: # ะฟะพัะปะตะดะฝะธะน ัะปะตะผะตะฝั‚ ะฝะต ั€ะฐะฒะตะฝ ั‚ะตะบัƒั‰ะตะผัƒ currentList.append(word) log.write("%s\n" % word) log.flush() 

How to proceed? Beginner, I can not guess.

  • In the question, use the minimum working sample code. Guess what your text_element_by_class_name () method returns is none other than you. - Alexey Reytsman
  • @Alexey, I wrote, for example, instead of name_elem_site = Word1. After a while Slovo2 and so on, there can be repetitions, the main thing in the record is to chop off consecutive identical words. - J. Doe
  • Remember the last word and check that the new word! = Last. - gil9red
  • If name_elem_site = 'Word1', then the rest of the code becomes meaningless, since the currentList in this case always consists of 1 element. If these are two different code fragments from different parts of the program, then it is worth mentioning this somehow. - Alexey Reytsman

2 answers 2

Based on the assumption that you somehow get a list, I can suggest using something like this:

 # ะ”ะพะฟัƒัั‚ะธะผ ัƒ ะฒะฐั ะบะฐะบ-ั‚ะพ ะฟะพะปัƒั‡ะธะปัั ะฒะพั‚ ั‚ะฐะบะพะน ัะฟะธัะพะบ initial_words_list = ['ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ3', 'ะกะปะพะฒะพ3', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ3', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ3', 'ะกะปะพะฒะพ3', 'ะกะปะพะฒะพ3', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ2'] result_list = [] # ัะพะทะดะฐะดะธะผ ะฟัƒัั‚ะพะน ัะฟะธัะพะบ, ะฒ ะบะพั‚ะพั€ั‹ะน ะฑัƒะดะตะผ ัะบะปะฐะดั‹ะฒะฐั‚ัŒ ะฝะตะฟะพะฒั‚ะพั€ััŽั‰ะธะตัั ัะปะพะฒะฐ previous_word = '' # ัะพะทะดะฐะตะผ ะฟัƒัั‚ัƒัŽ ั‚ะตะบัั‚ะพะฒัƒัŽ ะฟะตั€ะตะผะตะฝะฝัƒัŽ, ะฒ ะบะพั‚ะพั€ัƒัŽ ะฑัƒะดะตะผ ะทะฐะฟะธัั‹ะฒะฐั‚ัŒ ะฟั€ะตะดั‹ะดัƒั‰ะตะต ัะปะพะฒะพ for word in initial_words_list: if word != previous_word: # ะตัะปะธ ั‚ะตะบัƒั‰ะตะต ัะปะพะฒะพ ะฝะต ั€ะฐะฒะฝะพ ะฟั€ะตะดั‹ะดัƒั‰ะตะผัƒ result_list.append(word) # ะทะฐะฟะธัั‹ะฒะฐะตะผ ะตะณะพ ะฒ ัะฟะธัะพะบ-ั€ะตะทัƒะปัŒั‚ะฐั‚ previous_word = word print(result_list) # ['ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ3', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ3', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ3', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ2'] 
  • maybe I have a problem with my list at the very beginning. Because I can not even know what the words will be there. Those. generation on the go from the site. Therefore, initially, allegedly, the list is empty. After the appearance, I enter the data there. It turns out the list is not needed in this part? Those. it is easier to get SlovoN and write a value to the file, then open the file and if the last line = the new value received, then skip and wait for the next word. So what? - J. Doe
  • @ J.Doe, please provide the code that generates the list. In the code you provide, the list is first reset to zero, and then one element is added to it. - Alexey Reytsman

Try the function groupby from the standard library itertools

 from itertools import groupby l = ['ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ3', 'ะกะปะพะฒะพ4', 'ะกะปะพะฒะพ4'] new_l = [word for word, group in groupby(l)] print(new_l) # ะะฐะฟะตั‡ะฐั‚ะฐะตั‚: ['ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ3', 'ะกะปะพะฒะพ4'] 

PS: Learn standard libraries. They are not just added to the language itself.

  • I guess I did not set the condition correctly. Repetitions can be, but not in a row. # Prints: ['Word1', 'Word2', 'Word1', 'Word2', 'Word3'] This is necessary. # So it is impossible: ['Word1', 'Word1', 'Word2', 'Word2', 'Word1'] - J. Doe
  • Removing duplicates through groupby can be replaced by the set: print(set(l)) - gil9red
  • @ gil9red, new_l = set (l) also removes repetitions altogether l = ['ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ1', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ3', 'ะกะปะพะฒะพ1'] returns only {'ะกะปะพะฒะพ3', 'ะกะปะพะฒะพ2', 'ะกะปะพะฒะพ1'} and I need `{'Word1', 'Word2', 'Word1', 'Word2', 'Word3', 'Word1'}` - J. Doe
  • @ J.Doe and I said this to Alexander :) And how to solve this issue you wrote in the commentary on the issue - gil9red
  • one
    The groupby doesnโ€™t just "collapse" all repetitions, but only those that go in a row. Try it yourself on your data. - Xander