RexLeong
RexLeong

Reputation: 151

Split a string using a list of value at the same time

I have a string and a list:

src = 'ways to learn are read and execute.'
temp = ['ways to','are','and']

What I wanted is to split the string using the list temp's values and produce:

['learn','read','execute']

at the same time.

I had tried for loop:

for x in temp:
    src.split(x)

This is what it produced:

['','to learn are read and execute.']
['ways to learn','read and execute.']
['ways to learn are read','execute.']

What I wanted is to output all the values in list first, then use it split the string.

Did anyone has solutions?

Upvotes: 3

Views: 124

Answers (4)

just keep it simple

src = 'ways to learn are read and execute.'
temp = ['ways','to','are','and']
res=''
for w1 in src.split():
  if w1 not in temp:
    if w1 not in res.split():
      res=res+w1+" "
 print(res)

Upvotes: 0

Paritosh Singh
Paritosh Singh

Reputation: 6246

Loop solution. You can add conditions such as strip if you need them.

src = 'ways to learn are read and execute.'
temp = ['ways to','are','and']
copy_src = src
result = []
for x in temp:
    left, right = copy_src.split(x)
    if left:
        result.append(left) #or left.strip()
    copy_src = right
result.append(copy_src) #or copy_src.strip()

Upvotes: 0

Tony Stark
Tony Stark

Reputation: 36

This is a method which is purely pythonic and does not rely on regular expressions. It's more verbose and more complex:

result = []
current = 0
for part in temp:
    too_long_result = src.split(part)[1]
    if current + 1 < len(temp): result.append(too_long_result.split(temp[current+1])[0].lstrip().rstrip())
    else: result.append(too_long_result.lstrip().rstrip())
    current += 1
print(result)

You cann remove the .lstrip().rstrip() commands if you don't want to remove the trailing and leading whitespaces in the list entries.

Upvotes: 0

Kevin
Kevin

Reputation: 76194

re.split is the conventional solution for splitting on multiple separators:

import re

src = 'ways to learn are read and execute.'
temp = ['ways to','are','and']

pattern = "|".join(re.escape(item) for item in temp)
result = re.split(pattern, src)
print(result)

Result:

['', ' learn ', ' read ', ' execute.']

You can also filter out blank items and strip the spaces+punctuation with a simple list comprehension:

result = [item.strip(" .") for item in result if item]
print(result)

Result:

['learn', 'read', 'execute']

Upvotes: 7

Related Questions