Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a word in list and append to same list

Tags:

python

list

My List:

city=['Venango Municiplaity', 'Waterford ship','New York']

Expected Result:

city = ['Venango Municiplaity ', 'Waterford ship','New York','Venango','Waterford']

Common_words:

common_words = ['ship','municipality']

Scan all the items in My List and strip the common words and re-insert in the same list as shown in Expected Result.

I'm able to search the items which contains the common words but not sure how to replace that with blank and re-insert in My List.

My code so far:

for item in city:
    if(any(x in s.lower() for s in item.split(' ') for x in common_words)) :
like image 829
min2bro Avatar asked Aug 22 '18 07:08

min2bro


3 Answers

I have made a small code that works as expected:

city=['Venango Municiplaity', 'Waterford ship','New York']
comwo = ['ship','municipality']
for i, c in enumerate(city):
    for ii in comwo:
        if ii in c:
            city.append(city[i].replace(ii,""))
print(city)

Output:

['Venango Municiplaity', 'Waterford ship', 'New York', 'Waterford ']

Note:

The list you have made contains incorrect spelling.
Look at list city's first element VenangoMuniciplaity and second element of common_words municipality

If you want this in one-liner:

[city.append(city[i].replace(ii,"")) for ii in comwo for i, c in enumerate(city) if ii in c]

I used list comprehension to append to the list city.


Edit:

So if you also want to replace the space (if any) behind the word then I have made a separate code:

city=['Village home', 'Villagehome','New York']
comwo = ['home']
for i, c in enumerate(city):
    for ii in comwo:
        if ii in c:
            city.append(city[i].replace(" "+ii,"")) if city[i].replace(" "+ii,"") != city[i] else city.append(city[i].replace(ii,""))
print(city)

Output:

['Village home', 'Villagehome', 'New York', 'Village', 'Village']

If you need this in one-liner as well:

[city.append(city[i].replace(" "+ii,"")) if city[i].replace(" "+ii,"") != city[i] else city.append(city[i].replace(ii,"")) for ii in comwo for i, c in enumerate(city) if ii in c]
like image 175
Black Thunder Avatar answered Oct 19 '22 01:10

Black Thunder


I suggest you the following solution, using re.sub with flags=re.IGNORECASE to strip the common words ignoring the case:

import re

city = ['Venango Municipality', 'Waterford ship','New York']
common_words = ['ship','municipality']

toAppend = []

for c in city:
    for cw in common_words:
        if cw.lower() in c.lower().split():
            toAppend.append(re.sub(cw, "", c, flags=re.IGNORECASE).strip())

city += toAppend

print(city) # ['Venango Municipality', 'Waterford ship', 'New York', 'Venango', 'Waterford']

And here is the ONE-LINE STYLE solution using list comprehension, short but a bit less readable:

import re

city = ['Venango Municipality', 'Waterford ship','New York']
common_words = ['ship','municipality']

city += [re.sub(cw, "", c, flags=re.IGNORECASE).strip() for c in city for cw in common_words if cw.lower() in c.lower().split()]

print(city) # ['Venango Municipality', 'Waterford ship', 'New York', 'Venango', 'Waterford']
like image 37
Laurent H. Avatar answered Oct 19 '22 01:10

Laurent H.


You can try it, create new list to save there data should be added to your original list, and then concatenate result:

In [1]: city=['Venango Municiplaity', 'Waterford ship','New York']

In [2]: common_words = ['ship', 'municiplaity']

In [3]: list_add = []

In [4]: for item in city:
   ...:     item_words = [s.lower() for s in item.split(' ')]
   ...:     if set(common_words) & set(item_words):
   ...:         new_item = [s for s in item.split(' ') if s.lower() not in common_words]
   ...:         list_add.append(" ".join(new_item))
   ...:         

In [5]: city + list_add
Out[5]: ['Venango Municiplaity', 'Waterford ship', 'New York', 'Venango', 'Waterford']
like image 6
Brown Bear Avatar answered Oct 19 '22 02:10

Brown Bear