Skip to content Skip to sidebar Skip to footer

Incrementing The Digit By 1 In Python Using Isdigit()

i understand that by using the following syntax i can remove all the digit appearing in my sentence. How can i modify this to increment the digit or as matter of fact do any operat

Solution 1:

I would use regex, the .sub() method and a lambda function.

The regex can easily detects digits, and you can then replace them with the incremented value.

import re
s = "You would like to eat salad as meal 1 and fruits as meal 2 and pizza as meal 3 in your diet."

s = re.sub(r'(\d+)', lambda match: str(int(match.group()) + 1), s)
print(s)
# You would like to eat salad as meal 2 and fruits as meal 3 and pizza as meal 4 in your diet.

If you really need to use .isdigt(), you should first .split(), the sentence, then iterate through the words list and incremente the value if the element is an actual int. Finally, do not forget to .join() the results to get your string back. Somethink like this:

words = [str(int(w) + 1) if w.isdigit() else w for w in s.split()]
s = " ".join(words)

Solution 2:

The answer from @Delgan is great and words perfectly. I just thought I would add to it as I noticed you are making some mistakes using list comprehension, and I believe this might be the source to your problem.

List comprehension is a great feature to use. But there are a couple parts you should realize. See the picture below to see the parts of list comprehension broken up.

enter image description here

So when you create a list using another collection. The end part, where you are using an if statement is used for filtering, but you actually want everything in the previous list (all the words in the previous sentence) to be in the new list. You only want to increment all numbers in that sentence.

This means you should perform a function on certain elements. I see in your problem the part of list comprehension to perform a function on certain elements in the list (specifically, elements that are positive or negative integers) is what you want. This should be done in the list result field. Here is my example code:

# Function acting on all elements, increase integer values otherwise return original stringdefincrement_if_digit(word):
    if x.isdigit():
        value = int(x)
        value += 1returnstr(value)
    elif x[0] is'-'and x[1:].isdigit():
        value = int(x)
        value += 1returnstr(value)
    else:
        return word

# Example usage
sentence = "You would like to eat salad as meal 1 and fruits as meal 2 and pizza as meal 3 in your diet."
no_integers = [increment_if_digit(x) for x in sentence.split()]
print" ".join(no_integers)

which outputs:

You would liketo eat salad as meal 2and fruits as meal 3and pizza as meal 4in your diet. 

Solution 3:

The example is much easier with try and except to catch errors vs trying to build a small parser.

>>> stmt='You would like to eat salad as meal 1 and fruits as meal 2 and pizza as meal 3 in your diet'defincs(word, n):
    try:
        returnstr(int(word)+n)
    except ValueError:
        return word

>>> ' '.join([incs(w, 1) for w in stmt.split()])       
You would like to eat salad as meal 2and fruits as meal 3and pizza as meal 4in your diet     

Post a Comment for "Incrementing The Digit By 1 In Python Using Isdigit()"