0

I just want to be able to split both a + and - into a single array.

array = []
function = x+y-z
array = function.split("+")
array = function.split("-")

Expected output:

[x, y, z]

Obviously this isn't correct but can someone provide a real example?

2 Answers2

3

You can use module re:

>>>import re
>>>re.split(r'[+-]', 'x+y-z')
['x', 'y', 'z']
Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69
dragon2fly
  • 2,309
  • 19
  • 23
2

You can use regex for the split:

import re

function = 'x+y-z'
array = re.split("\+|\-", function)
print array # prints ['x', 'y', 'z']
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129