I am looking for a nice, efficient and pythonic way to go from something like this:
('zone1', 'pcomp110007')
to this:
'ZONE 1, PCOMP 110007'
without the use of regex
if possible (unless it does make a big difference that is..). So turn every letter into uppercase, put a space between letters and numbers and join with a comma.
What i wrote is the following:
tags = ('zone1', 'pcomp110007')
def sep(astr):
chars = ''.join([x.upper() for x in astr if x.isalpha()])
nums = ''.join([x for x in astr if x.isnumeric()])
return chars + ' ' + nums
print(', '.join(map(sep, tags)))
Which does produce the desired result but looks a bit too much for the task.
The tuple might vary in length but the numbers are always going to be at the end of each string.
Python's built-in function tuple() converts any sequence object to tuple. If it is a string, each character is treated as a string and inserted in tuple separated by commas.
Regex does help. This should always work:
import re
tags = ('zone1', 'pcomp110007')
def sep(s):
word = re.split(r'\d', s)[0]
return word.upper() + " " + s[len(word):]
print(', '.join(map(sep, tags)))
My thoughts:
Keep sep
a normal function like it is in your original code for readability / maintenance, but also leverage re
as suggested in Abdou's answer.
import re
tags = ('zone1', 'pcomp110007')
def sep(astr):
alpha, num = re.match('([^\d]+)([\d]+)', astr).groups()
return '{} {}'.format(alpha.upper(), num)
print(', '.join(map(sep, tags)))
Edit: Note that if you prefer, I think it would also be reasonable to just return:
return alpha.upper() + ' ' + num
Or older style string formatting:
return '%s %s' %(alpha.upper(), num)
Whatever you're most comfortable with.
Using re
:
import re
tupl = ('zone1', 'pcomp110007')
", ".join(map(lambda x: " ".join(re.findall('([A-Z]+)([0-9])+',x.upper())[0]), tupl))
#'ZONE 1, PCOMP 7'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With