5

I have strings that have optional numbers and letters, such as '01a', 'b' and '02'. These strings have always two parts, numbers on the left, and letters on the right side. I'd like to split these strings to get the numbers and letters separately. How can I define mySplit in a way to get this result?

>>> map(mySplit, ['01123absd', 'bsdf', '02454'])

[('01123', 'absd'), (None, 'bsdf'), ('02454', None)]
3
  • 1
    Will it always be formatted numbers then letters?
    – thegrinner
    Feb 20, 2014 at 19:56
  • Will there always be only two parts?
    – LucasB
    Feb 20, 2014 at 20:05
  • Yes, there will be always two parts.
    – msampaio
    Feb 20, 2014 at 20:09

5 Answers 5

6

You can use regular expressions for this. What we want is:

  • a string that starts with 0 or more digits,
  • a string that ends with 0 or more letters.

Note that the regex will create named groups, it is also compiled once to be more efficient every time is it called.

import re
regex = re.compile("^(?P<numbers>\d*)(?P<letters>\w*)$")

def myFunction(entry):
  (numbers, letters) = regex.search(entry).groups()
  return (numbers or None, letters or None)

map(myFunction, ['01123absd', 'bsdf', '02454'])

The call on the last line gives the following output:

[('01123', 'absd'), (None, 'bsdf'), ('02454', None)]
4

Similar to jramirez answer, just a bit shorter:

def myFunction(s):
    return (''.join(c for c in s if c.isdigit()) or None, 
            ''.join(c for c in s if c.isalpha()) or None)

A little shorter still using filter:

def myFunction(s):
    return (''.join(filter(str.isdigit, s)) or None, 
            ''.join(filter(str.isalpha, s)) or None)

Output:

print(*map(myFunction, ['01123absd', 'bsdf', '02454', '012abc345def']))
('01123', 'absd') (None, 'bsdf') ('02454', None) ('012345', 'abcdef')
1
  • Great solution as it's much faster than using regexp.
    – misantroop
    Aug 20, 2019 at 20:51
3

You can use regular expressions here:

import re

def myFunction(numbers_and_letters):
    m = re.match(r"([a-z]*)([0-9]*)", numbers_and_letters)
    return tuple(v == "" and None or v for v in m.groups())
3
  • should it not use stars since neither sort is necessary? Feb 20, 2014 at 20:00
  • Yes, you're right. I need to change the ''s to None also.
    – apai
    Feb 20, 2014 at 20:01
  • return tuple(v == "" and None or v for v in m.groups()) Feb 20, 2014 at 20:06
2

If you don't want to use regex this is a solution:

def split_num_str(my_str):
    num = [x for x in my_str if x.isdigit()]
    num = "".join(num)

    if not num:
        num = None

    my_str = [x for x in my_str if x.isalpha()]
    my_str = ''.join(my_str)

    if not my_str:
        my_str = None

    return num, my_str

m = map(split_num_str, ['01123absd', 'bsdf', '02454'])
print m

result = [('01123', 'absd'), (None, 'bsdf'), ('02454', None)]

0
import re


def myFunction(t):
    m = re.match(r"([a-z]*)([0-9]*)", t)
    x,y = m.groups()
    if x=='': x= None    
    if y=='': y= None    
    return ((x,y))

for s in ['01123absd', 'bsdf', '02454']:
    print myFunction(s)

produces:

(None, '01123')
('bsdf', None)
(None, '02454')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.