[Tutor] how to separate the digital and the alphabeta
d at davea.name
Thu Nov 3 18:05:50 CET 2011
On 11/03/2011 12:30 PM, lina wrote:
> ['1AB','57GL', '76LE']
> How can I extract 1, 57 , 76 out?
> except the one I tried as:
>>>> for i in range(len(a)):
> are there some way to tell the difference between the [0-9] and [A-Z],
In the last thread, somebody else gave you a regular expression that
would separate the digits out. I avoid regex'es like the plague, for no
good reason, so I can't help with that approach.
You can tell a particular string is all digits by using the isdigit()
function. So some sort of while loop will find it. Perhaps (untested):
"""find the largest prefix of the given string that is all digits.
return the integer so produced"""
res = "0"
while(res and (res + item).isdigit():
res += item
return int(res, 10)
But your (tiny) sample always has two letters after the digits. Is that
the rule? If so, you've got a simple version
b = item[:-2]
More information about the Tutor