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,230
  • 10
  • 37
  • 68
dragon2fly
  • 1,994
  • 16
  • 20
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
  • 51,812
  • 11
  • 81
  • 120