Tuesday 15 February 2011

python - split string update numbers only -



python - split string update numbers only -

i have function using raw_input getting sentence user. sec function splits sentence trying update numbers on sentence. split sentence:

['you', 'are', '42', 'this', 'year']

i trying update 42 43 homecoming , print 'you 43 year'

i able pull number using isdigit() can't increment it. have far:

def getdigits(sentence): in sentence: if i.isindigit(): = +1 homecoming sentence

numbers in python immutable objects, when i = i+1, creating new object. new object not part of original sentence object. also, '42' string. can't apply numeric add together operation on it. need convert integer first.

this need -

def getdigits(sentence): idx, value in enumerate(sentence): if value.isdigit(): sentence[idx] = int(value) +1 homecoming sentence

python string return add updates

No comments:

Post a Comment