Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Capture Substring from list elements

Having list of strings as below:

    ['Communcation between: ACC_Verkehrssinn and SG_ACC_22\nUsing testing method: MinMaxMid', 
     'Communcation between: ACC_Status_Laengs and SG_ACC_22\nUsing testing method: MinMaxMid',
     'Signal Name:WLA_FC1_Obj10angleLeft\nCommuncation between: SG_WLA_FC1_Obj01_30 and 
      SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid', 
     'Signal Name:WLA_FC1_Obj10angleRight\nCommuncation between: SG_WLA_FC1_Obj01_30 and 
      SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid']

Want to read list elements which contains ‘Signal Name:’ and capture its value. Here in this case output should be:

WLA_FC1_Obj10angleLeft
WLA_FC1_Obj10angleRight

Any help is appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We can use a list comprehension here:

inp = ['Communcation between: ACC_Verkehrssinn and SG_ACC_22\nUsing testing method: MinMaxMid', 'Communcation between: ACC_Status_Laengs and SG_ACC_22\nUsing testing method: MinMaxMid', 'Signal Name:WLA_FC1_Obj10angleLeft\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid', 'Signal Name:WLA_FC1_Obj10angleRight\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid']
output = [re.search(r'Signal Name:\s*(\S+)', x).group(1) for x in inp if 'Signal Name:' in x]
print(output)

This prints:

['WLA_FC1_Obj10angleLeft', 'WLA_FC1_Obj10angleRight']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading