Skip to content Skip to sidebar Skip to footer

Omitting First Word And Removing Words Which Are Part Of Another Word

Suppose I have a list of strings like - lst = ['bed frame', 'queen bed', 'queen bedding', 'queen bed frame', 'queen bedroom', 'bedroom', 'queen b', 'queen be', 'queen bed fra', 'qu

Solution 1:

  • First, create a new list with all the sentences split into words. (let's call it search_lst).
  • you will then require a nested loop. First to iterate through your main list and second to iterate through words.
  • append the word if the word is not in the search_lst.

Example:

lst = ['queen bed', 'queen be', 'queen b',
       'queen bed frame', 'queen bed fr', 'queen bed fra']

lst2 = []

search_lst = [y for z in lst for y in z.split()]

for x in lst:
    temp = []
    for y in x.split():
        
        if y != x andnotany(y in z for z in search_lst if y!=z):
            temp.append(y)

    lst2.append(' '.join(temp))

print(lst2)

output:

['queen bed', 'queen', 'queen', 'queen bed frame', 'queen bed', 'queen bed']

Post a Comment for "Omitting First Word And Removing Words Which Are Part Of Another Word"