简体   繁体   中英

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?

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']

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.

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()

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM