Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Produce a string from a tuple

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.

like image 694
Ma0 Avatar asked Nov 15 '16 17:11

Ma0


People also ask

Can a string be in a tuple?

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.


3 Answers

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)))
like image 177
L3viathan Avatar answered Nov 06 '22 10:11

L3viathan


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.

like image 30
Brian McFarland Avatar answered Nov 06 '22 12:11

Brian McFarland


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'
like image 29
Abdou Avatar answered Nov 06 '22 12:11

Abdou