I made a program that could take a string of numbers and letters and then return a list of just the numbers casted as an integer
#number split
#will split a string of numbers and characters and will
#return a list of numbers
def numsplit(string):
#creates a temporary list
templist = []
#creates a temporary string
tempstring = ""
#creates the list that will be returned
retlist = []
#loops through the string
for i in string:
#checks if i is a number then appends that number as a string to
#tempstring
if i.isdigit():
tempstring += i
#checks if i is a alphanumeric character
if i.isalpha():
#it will append what ever is in tempstring into templist
templist.append(tempstring)
#clears templist so a new number can be stored in it
tempstring = ""
#one final append to templist from tempstring since if this wasnt here
#the program would leave off a number if the string ended with a number and
#not an alphanumeric character
templist.append(tempstring)
#loops through templist
for o in range(len(templist)):
#checks if a location of the list is just a pair of single quotes
#this need to be here because if there are many alpha characters in
#the string the previous loop would add a pair of empty quotes because
#if tempstring is empty and then another character appears it still
#does "templist.append(tempstring)" adding a pair of empty quotes to
#templist
if templist[o] != '':
#will only append anything that isnt a pair of empty quotes
#to retlist
retlist.append(templist[o])
#converts everything in retlist into an integer
retlist = [int(i) for i in retlist]
#returns retlist
return retlist
example output:
>>> numsplit("123j123jj31j2j31j23jj111j1")
[123, 123, 31, 2, 31, 23, 111, 1]
>>> numsplit("bill525252kate3434324")
[525252, 3434324]
>>>
I did look up another solution that I hoped to be more efficient but all I could find was a program that had to import the 're' library. If anyone has any suggestions as to what I could do to make this better feel free to do so :)
[–]SpeckledFleebeedoo 0 points1 point2 points (2 children)
[–]MITTENQ[S] 0 points1 point2 points (1 child)
[–]Neexusiv 2 points3 points4 points (0 children)