1

I want to create a list given a string such as 'b123+xyz=1+z1$' so that the list equals ['b123', '+', 'xyz', '=', '1', '+', 'z1', '$']

Without spaces or a single repeating pattern, I do not know how to split the string into a list.

I tried creating if statements in a for loop to append the string when it reaches a character that is not a digit or letter through isdigit and isalpha but could not differentiate between variables and digits.

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
besusdd
  • 13
  • 2
  • What is the criterion you want to split the string by? Is it simply alphanumerucal values interleaved with operators? – Maciej Gol Oct 31 '22 at 23:58
  • If this is an equation, why do you want `b123+xyz=1+z1$` to be `['b123', '+', 'xyz', '=', '1', '+', 'z1', '$']` ? Why not `['b123', '+', 'xyz', '=', '1', '+', 'z1$']`? Then split on operators. – Carl_M Nov 01 '22 at 00:00
  • is `**` potentially a valid operator? – bn_ln Nov 01 '22 at 01:14

5 Answers5

2

You can use a regular expression to split your string. This works by using positive lookaheads and look behinds for none word chars.

import re

sample = "b123+xyz=1+z1$"
split_sample = re.split("(?=\W)|(?:(?<=\W)(?!$))", sample)
print(split_sample)

OUTPUT

['b123', '+', 'xyz', '=', '1', '+', 'z1', '$']

REGEX EXPLAIN enter image description here

Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
2

Another regex approach giving the same result is:

split_sample = re.split(r"(\+|=|\$)", sample)[:-1]

The [:-1] is to remove the final empty string.

user19077881
  • 3,643
  • 2
  • 3
  • 14
  • Upv but wonder if you’d not be better off with `li = [v for in if v]`. That would fix trailing *and* leading empties and avoid unempty deletions. – JL Peyret Nov 01 '22 at 00:58
0
"""
Given the equation b123+xyz=1+z1$, break it down
into a list of variables and operators
"""
operators = ['+', '-', '/', '*', '=']

equation = 'b123+xyz=1+z1$'
equation_by_variable_and_operator = []
text = ''
for character in equation:
    if character not in operators:
        text = text + character
    elif character in operators and len(text):
        equation_by_variable_and_operator.append(text)
        equation_by_variable_and_operator.append(character)
        text = ''
# For the final variable
equation_by_variable_and_operator.append(text)
print(equation_by_variable_and_operator)

Output

['b123', '+', 'xyz', '=', '1', '+', 'z1$']
Carl_M
  • 861
  • 1
  • 7
  • 14
0

A straight-forward regex solution is;

equation = "b123+xyz=1+z1$"
equation_list = re.findall(r'\W+|\w+', equation)
print(equation_list)

This would also work with strings such as -b**10.

Using re.split() returns empty strings at the start and end of the string from the delimiters at the start and end of the string (see this question). To remove them, they can be filtered out, or otherwise look-behind or look-ahead conditions can be used which add to the pattern's complexity, as earlier answers to this question demonstrate.

bn_ln
  • 1,648
  • 1
  • 6
  • 13
0

Well my answer seems to not be the easiest among them all but i hope it helps you.

data: str = "b123+xyz=1+z1$"

symbols: str = "+=$"

merge_text: str = ""
for char in data:
    if char not in symbols:
        merge_text += char
    else:
        # insert a unique character for splitting
        merge_text += ","
        merge_text += char
        merge_text += ","

final_result: list = merge_text.split(",")
Mane Motha
  • 11
  • 4