I've three lists, one is black list
of words and one is frequency
and the other new words
i want to check if one of new_words
words in black list then append 1 to frequency
if not append 0 to frequency
, but i want to after all words been checked,
i have to check all words in the speech then if one of the words inside the black list then just append once not for the entire for loop
here is what im trying :
frequency = []
the_speech = 'hello how are you, what a shit you said'
list_of_speech = the_speech.split(' ')
print(list_of_speech)
black_lists = ['fuck', 'shit'] #more and more
for s in list_of_speech:
if s in black_lists:
frequency.append(1)
print('inserted into black list')
break
else:
frequency.append(0)
print('inserted, wihtout finding a black list word')
break
but this doesnt work as i expect, it always decide base one the first element of the list, how to make it complete the all loop then do the append, thank you in advance ..
CodePudding user response:
In your code, you use break at every end of statements, which will make it run one time.
Method 1: (Original)
Just remove the break and it should work. But this can be easily bypassed by using a .
after it or using uppercase.
UPDATED:
- Removed useless thing
contain_bad_word = False
the_speech = 'hello how are you, what a shit you said'
list_of_speech = the_speech.split(' ')
print(list_of_speech)
black_lists = ['fuck', 'shit'] #more and more
for s in list_of_speech:
if s in black_lists:
contain_bad_word = True
print('blah blah')
break
Method 2: (more accurate, recommend)
Rabinzel question is the same (I updated after him/her so I wann put in here) This method will loop though the black listed and check is there any in the string.
contain_bad_word = False
the_speech = 'hello how are you, what a shit you said'
list_of_speech = the_speech.split(' ')
print(list_of_speech)
black_lists = ['fuck', 'shit'] #more and more
for s in black_lists:
if s.upper() in the_speech.upper(): #No uppercase bypass
contain_bad_word = True
print('blah blah')
break
CodePudding user response:
Ok, I'll give it another shot. Here you loop through the list and check everytime for a bad word. If you find one bad word you break out the loop. after that you check if no_bad_word
is True/False and print a final statement.
the_speech = 'hello how are you, what a shit you said'
list_of_speech = the_speech.split(' ')
print(list_of_speech)
black_lists = ['fuck', 'shit'] #more and more
no_bad_word = True
for s in list_of_speech:
if s in black_lists:
print('User had a bad word in his speech')
no_bad_word = False
break
if no_bad_word:
print('the speech has no bad words of the black list')
EDIT:
I have another way for you with any()
:
it checks the list on the black_list and in the end, if there is only once True
then there was a bad word in it.
decision = any(word.lower() in black_lists for word in list_of_speech)
if decision:
print('User had a bad word in his speech')
else:
print('the speech has no bad words of the black list')
CodePudding user response:
you can just do a double for loop like this
frequency = []
the_speech = 'hello how are you, what a shit you said'
list_of_speech = the_speech.split(' ')
print(list_of_speech)
black_lists = ['fuck', 'shit'] #more and more
for cword in list_of_speech:
for blw in black_lists:
if cword == blw:
frequency.append(1)
print("You have a blacklisted word")
else:
frequency.append(0)
and then you can add it to your list