Home > Back-end >  split list elements iteratively
split list elements iteratively

Time:04-27

I want to split a list names element. More precicely i only want to split the strings with Oscar Muller

names = ['Oscar Muller Some other Name', 'Oscar Muller', 'Peter Pan']
expected_names = ['Oscar Muller', 'Some other Name', 'Oscar Muller', 'Peter Pan']

d = "Oscar Muller "
for line in names:
    s = [e d for e in line.split(d) if e]

That didnt do anything.

[list(filter(None, re.split(r'Oscar\sMuller\s', i))) for i in names]

didnt do anything either.

d1 = re.compile(r"Oscar\sMuller\s")
d = d1.search(names)
for line in names:
    if d:
        s = [e d for e in line.split(d) if e]

but it caused issues with input .split(). Error: TypeError: must be str or None, not re.Pattern. So i changed it to process each list element.

d1 = re.compile(r"Oscar\sMuller\s")
d = list(filter(d1.match, names))
for line in names:
    if d:
        s = [e d for e in line.split(d) if e]

But it didnt work either, returning TypeError: must be str or None, not list

Question: What am i doing wrong?

CodePudding user response:

You can also use list comprehension to make it one line:

import re
[j for i in [re.split(r"(?<=Oscar Muller)", k) for k in names] for j in i if j]

CodePudding user response:

Essentially, what you need to do is generate 1 or 2 item sublists for each item in the original list, and then flatten the list into a single iterable.

A couple ways you could do this. You could use a generator function, or some clever use of itertools

import re

def my_generator(names):
    for name in names:
        sublist = re.split(r"(?<=Oscar Muller) ", name)
        yield from sublist

names = ['Oscar Muller Some other Name', 'Oscar Muller', 'Peter Pan']
expected_names = list(my_generator(names))

Or you could one-liner it with itertools:

import itertools
import re

names = ['Oscar Muller Some other Name', 'Oscar Muller', 'Peter Pan']
expected_names = list(itertools.chain.from_iterable(re.split(r"(?<=Oscar Muller) ", s) for s in names))
  • Related