1

I am working on a routing project. The route looks like this "CNSHG(B)-PAMIT(R)-COCTG(B)-USHOU(R)-COCTG(B)-USMSY" and I want to break it into a nested list. Also, a route contains multiple segments for example CNSHG-PAMIT is one segment transported using B and then PAMIT-COCTG transported using R i.e, Rail, and so on.

Input:

"CNSHG(B)-PAMIT(R)-COCTG(B)-USHOU(R)-COCTG(B)-USMSY"

The output should be like this:

[[CNSHG, PAMIT, B],[PAMIT, COCTG, R],[COCTG, USHOU, B],[USHOU, COCTG, R],[COCTG, USMSY, B]]

I have tried using regex and the below codes but it didn't work.

route.str.extract('(.)\s\((.\d+)')

Thanks a lot.

3
  • 1
    Why are you using (.\d+)? There are no digits in your input. Commented Dec 23, 2021 at 13:15
  • Also, your final destination USMSY doesn't have a means of transportation following it (B, R, or something else). Will that always be the case? Commented Dec 23, 2021 at 13:20
  • @MattDMo The transport mode is written in brackets with the first transport port, so there is no mode on the last port because that is the destination. Commented Dec 23, 2021 at 13:24

1 Answer 1

2

You can use

import pandas as pd
df = pd.DataFrame({'col':["CNSHG(B)-PAMIT(R)-COCTG(B)-USHOU(R)-COCTG(B)-USMSY"]})
df['result'] = df['col'].str.findall(r'(\w+)\((?=[^()]*\)-(\w+))([^()]*)\)')

Output of df['result']:

[('CNSHG', 'PAMIT', 'B'), ('PAMIT', 'COCTG', 'R'), ('COCTG', 'USHOU', 'B'), ('USHOU', 'COCTG', 'R'), ('COCTG', 'USMSY', 'B')]

See the regex demo. Details:

  • (\w+) - one or more word chars
  • \( - a ( char
  • (?=[^()]*\)-(\w+)) - a positive lookahead that requires (immediately to the right of the current location):
    • [^()]* - zero or more chars other than ( and )
    • \)- - a )- string
    • (\w+) - Group 2: one or more word chars
  • ([^()]*) - Group 3: zero or more chars other than ( and )
  • \) - a ) char.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.