Piyush Panwar
Piyush Panwar

Reputation: 15

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 operation on it....

no_integers = [x for x in list_add_one_city if not (x.isdigit() or x[0] == '-' and x[1:].isdigit())]

for example... given stmt You would like to eat salad as meal 1 and fruits as meal 2 and pizza as meal 3 in your diet.

need to be translated to (using ISDIGIT()): You would like to eat salad as meal 2 and fruits as meal 3 and pizza as meal 4 in your diet.

Upvotes: 0

Views: 691

Answers (3)

napkinsterror
napkinsterror

Reputation: 1965

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 string
def increment_if_digit(word):
    if x.isdigit():
        value = int(x)
        value += 1
        return str(value)
    elif x[0] is '-' and x[1:].isdigit():
        value = int(x)
        value += 1
        return str(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 like to eat salad as meal 2 and fruits as meal 3 and pizza as meal 4 in your diet. 

Upvotes: 2

dawg
dawg

Reputation: 103864

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'

def incs(word, n):
    try:
        return str(int(word)+n)
    except ValueError:
        return word

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

Upvotes: 0

Delgan
Delgan

Reputation: 19627

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)

Upvotes: 3

Related Questions