1

I would like to cut the list elements after a chosen delimiters(many at once): '-', ',' and ':'

I have an example list:


list_1 = ['some text – some another', 'some text, some another', 'some text: some another']

I'd like to cut the list elements(strings in that case) so that it will return the following output:

splitted_list = ['some text', 'some text', 'some text']

I already tried with split() but it only takes 1 delimiter at a time:

splited_list = [i.split(',', 1)[0] for i in list_1]


I would prefer something which is more understandable for me and where I could decide which delimiter to use. For example, I don't want to cut string after - but after -.

List of delimiters:

: , - , ,

Note that - has space before and after, : only after, just like , .

anubhava
  • 761,203
  • 64
  • 569
  • 643
Michal K
  • 69
  • 1
  • 7
  • `splited_list = [i.split(',', 1)[0].split('-', 1)[0].split(':', 1)[0] for i in list_1]` to split new "result" again or just replace 2 delims into third and split with it: `splited_list = [i.replace('-',',').replace(':',',').split(',', 1)[0] for i in list_1]` – h4z3 Dec 16 '21 at 11:09

1 Answers1

3

You may use this regex in re.sub and replace it with an empty string:

\s*[^\w\s].*

This will match 0 or more whitespace followed by a character that is not a whitespace and not a word character and anything afterwards.

import re

list_1 = ['some text – some another', 'some text, some another', 'some text: some another']
delims = [',', ':', ' –']
delimre = '(' + '|'.join(delims) + r')\s.*'
splited_list = [re.sub(delimre, '', i) for i in list_1]

print (splited_list)

Output:

['some text', 'some text', 'some text']
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Yes, your solution working perfectly fine, although I would prefer something which is more understandable for me and where I could decide which delimiter to use. For example, I don't want to cut string after '-' but after ' - '. Don't get me wrong - your code is very OK - thanks for that - I just look for a more manageable example. – Michal K Dec 16 '21 at 15:16
  • Please specify which delimiters you want to include and answer can be tweaked easily. – anubhava Dec 16 '21 at 15:20
  • ': ' , ' - ' , ', ' Note that ' - ' has space before and after, ': ' only after, just like ', '. That's why I needed more manageable code where I could simply add them. Thank you. – Michal K Dec 16 '21 at 15:31
  • ok check my updated answer now – anubhava Dec 16 '21 at 15:40
  • anubhava any chance you could shortly explain what this code does - delimre = '(' + '|'.join(delims) + r')\s.*' – Michal K Dec 18 '21 at 10:05
  • Please print `delimre` right after this line to see the regex string it is forming. `'|'.join(delims)` joins each element of `delims` array using `|` as delimiter. – anubhava Dec 18 '21 at 10:11