Home > Enterprise >  Python Split Strings While Preserving Order?
Python Split Strings While Preserving Order?

Time:06-28

I have a list of strings in python, where I need to preserve order and split some strings.

The condition to split a string is that after first match of : there is a none space/new line/tab char.

For example, this must be split:

example: Test to ['example':, 'Test']

While this stays the same: example: , IGNORE_ME_EXAMPLE


Given an input like this: ['example: Test', 'example: ', 'IGNORE_ME_EXAMPLE']

I'm expecting: ['example:', 'Test', 'example: ', 'IGNORE_ME_EXAMPLE']

Please Note that split strings are yet stick to each other and follow original order.

Plus, whenever I split a string I don't want to check split parts again. In other words, I don't want to check 'Test' after I split it.

To make it more clear, Given an input like this:

['example: Test::YES']

I'm expecting: ['example:', 'Test::YES']

CodePudding user response:

You can use regular expressions for that:

import re

pattern = re.compile(r"(. :)\s ([^\s]. )")

result = []
for line in lines:
    match = pattern.match(line)
    if match:
        result.append(match.group(1))
        result.append(match.group(2))
    else:
        result.append(line)

CodePudding user response:

You can use nested loop comprehension for the input list:

l = ['example: Test::YES']

l1 = [j.lower().strip() for i in l for j in i.split(":", 1) if j.strip().lower() != '']
print(l1)

Output:

['example', 'Test::YES']

CodePudding user response:

you need to iterate over your list of words, for each word, you need to check if : present or not. if present the then split the word in 2 parts, pre : and post part. append these pre and post to final list and if there is no : in word add that word in the result list and skip other operation for that word

# your code goes here
wordlist = ['example:', 'Test', 'example:         ', 'IGNORE_ME_EXAMPLE']
result = []
for word in wordlist:
    index = -1
    part1, part2 = None, None
    if ':' in word:
        index = word.index(':')
    else:
        result.append(word)
        continue

    part1, part2 = word[:index 1], word[index 1:]
    if part1 is not None and len(part1)>0:
        result.append(part1)
    if part2 is not None and len(part2)>0:
        result.append(part2)
    
print(result)
    

output

['example:', 'Test', 'example:', '         ', 'IGNORE_ME_EXAMPLE']
  • Related