• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Find and replace a string using another list of string in python

Resolved: Find and replace a string using another list of string in python

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have a text
I need to find US and put it at the end: like this

output: 3200 NORTHLINE AVE STE 360GREENSBORO27408-7611 NC US


However, what if I have lookup as a list like below:
and multiple input as a list like below:
I need to find the country for each input list and put it at the end of a string for each list.
I tried many methods but couldn’t do. Your help will be greatly appreciated.
Thanks

Answer:

You can start by breaking the string down into tokens:
If you want to put a country code at the end you can sort the list of tokens using a key sort value. Example:
To put this back into a string you can use a join
To make this work for a variety of country codes, you only need to slightly modify the key
Putting this altogether:

If you have better answer, please add a comment about this, thank you!

list pandas python replace
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: navigate and redirect not redirecting to path in react router

24/03/2023

Resolved: Time Complexity: Find the number of people who know a secret leetcode 2327

24/03/2023

Resolved: Best way to create 3d matrix of variables in PULP

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.