0

I have a exclusion_list = ['and', 'or', 'not']

My string is 'Nick is not or playing football and or not cricket'

  • In the string if exclusion_list is coming together then it should take first one

My expected out is 'Nick is not playing football and cricket'

Code is below

search_value = 'Nick is not or playing football and or not cricket'
exclusion_list =  ['and', 'or', 'not']
search_value_2 = search_value.split(' ')
for i in range(len(search_value_2)):
    if search_value_2[i] in exclusion_list and search_value_2[i+1] in exclusion_list:
        search_value_2.replace(search_value_2[i+1], '')
' '.join(search_value_2)

As from suggestion i have modified the code

search_value = 'Nick is not or playing football and or not cricket'
exclusion_list =  ['and', 'or', 'not']
search_value_2 = search_value.split(' ')
for i in range(0,len(search_value_2)-2):
    if search_value_2[i] in exclusion_list and search_value_2[i+1] in exclusion_list:
        search_value_2.remove(search_value_2[i+1])
' '.join(search_value_2)

My out>> 'Nick is not playing football and not cricket'

Expected out> 'Nick is not playing football and cricket'

sim
  • 524
  • 3
  • 14

0 Answers0