[Tutor] how to separate the digital and the alphabeta

Dave Angel d at davea.name
Thu Nov 3 18:05:50 CET 2011

On 11/03/2011 12:30 PM, lina wrote:
> Hi,
> ['1AB','57GL', '76LE']
> How can I extract 1, 57 , 76 out?
> except the one I tried as:
>>>> for i in range(len(a)):
> 	print(a[i][:-2])
> 1
> 57
> 76
> 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):

def getkey(item):
     """find the largest prefix of the given string that is all digits.  
return the integer so produced"""
     res = "0"
     while(res and (res + item[0]).isdigit():
         res += item[0]
     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
def  getkey(item):
      b = item[:-2]
     return int(b)



More information about the Tutor mailing list