аватар question@mail.ru · 01.01.1970 03:00

A way out of several cycles

There is an input list, the elements of which consist of a certain text. It is necessary to check whether there are words from one list in the elements of the list from one list, and if there are words from one, and there is no one from the other, then get a certain message.
You can’t complete several cycles after receiving the result and go to the next list element

  Some_TEXT = [ 'sense text' ,  'Another text' ,  'one me '] whitelist = [' Another ', ' me '] blacklist = [' sense ",  'Else' ]   for  text  in  side_text:   for  items  in  blacklist:   if  text.find (items)! = - 1 :   for  item  in  Whitelist:   if  text.find (item) == - 1 :   print  ( 'found!' )   else :   print  ( '' '' not Found ')     

I also ask you to forgive for possible errors or typos in the code.

аватар answer@mail.ru · 01.01.1970 03:00

Option one, ugly

to get out of the cycle to its normal completion, the operator break :

   for  item  in  items:   if> some_condition:   break      

in order to get out of several cycles according to the condition that is executed in the inteal cycle, the output flag is used, which initially matters false , and if necessary, exit the cycle prematurely accepts the value True :

  Flag =  false    for  than  in  out_list:   for  inner  in  inner_list:   if  some_condition:  flag =  true    break    if  flag:   Break      

Thus, you can get out of any number of embedded cycles, you just need to add a check to the output flag.

The second, beautiful

you should change the structure of your code a little. To begin with, make checks on the entry of words from the blacklist and Whitelist in a separate function:

   def   containes_words  ( text, words_list ):   for  word  in  buts_list:   if  text.find (word)! = - 1 :   retu   true    retu   false      

now check for the entry of words from Whitelist Blacklist will look like this:

   for  text  in  in  some_text:   if  containe (text, Whitelist)  and   not  containe (text, BlackList):   print  ()   else :   print  ( "" Not Found "" )   

expressions not contacts_words (Text, Blacklist) , since, whatever it is, the meaning of the expression containe (Text, Whitelist) and NOT) and NOT Contains_Words (Text_blacklist) will always be false

.

Latest

Similar