Home > Net >  Using regex match from list to replace string with match
Using regex match from list to replace string with match

Time:09-22

I have 2 lists and would like to have matches from list_2 replace the entire string in list_1.

list_1
0   Univ Miami, Sch Med, Miami Project Cure Paralysis
1   Sch Med, Dept Neurol Chicago  Surg, 
2   Univ London, Sch Med, Dept Physiol & Biophys


list_2
0 New York
1 Chicago
2 London
3 Miami
...

Using something like list_1.replace('(?i)(Miami)', 'test', regex = True) I can only replace 'Miami' with 'test' but can not use list_2 as input pattern nor replace the entire string with the match.

What I would like to get as a result is:

list_1
0   Miami
1   Chicago
2   London 

I hope that makes sense, Thanks in advance

CodePudding user response:

You do not need regular expressions for this.

Taking back your example

list_1 = ["Univ Miami, Sch Med, Miami Project Cure Paralysis",
"Sch Med, Dept Neurol Chicago  Surg,",
"Univ London, Sch Med, Dept Physiol & Biophys"]

list_2 = ["New York", "Chicago", "London", "Miami"]

You can define a function, find_city, that checks if a city name is present in your titles (list_1), and return the city name if it is found:

def find_city(title, list_cities):
    title_lower = title.lower()
    for city in list_cities:
        if city.lower() in title_lower:
            return city

Things to note:

  • the use of the in operator to check if a string is contained in another; much simpler than using regular expressions.
  • we are putting everything in lowercase (title and city names) before performing the search.

Finally you can use a list comprehension to build your desired output:

list_3 = [find_city(title, list_2) for title in list_1]
print(list_3)
>>> ['Miami', 'Chicago', 'London']

CodePudding user response:

Thanks a lot, that really helps:

  • Related