【问题标题】:incrementing the digit by 1 in python using isdigit()在python中使用isdigit()将数字加1
【发布时间】:2015-11-18 22:01:39
【问题描述】:

我明白,通过使用以下语法,我可以删除句子中出现的所有数字。我如何修改它以增加数字或事实上对其进行任何操作....

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

例如... 给定的 stmt 您想在饮食中将沙拉作为第一餐,将水果作为第二餐,将披萨作为第三餐。

需要翻译成(使用 ISDIGIT()): 您想在饮食中将沙拉作为第 2 餐,将水果作为第 3 餐,将披萨作为第 4 餐。

【问题讨论】:

  • 更具体的def PrintAddOneMealNumSentence(sentence):list_add_one_meal = sentence.split()print list_add_one_mealadd_one_integer = [x for x in list_add_one_meal if not (x.isdigit() or x[0] == '-' and x[1:].isdigit())]print add_one_integer
  • 我仍然对你在这里究竟想做什么感到有点困惑。您能否发布list_add_one_cityno_integers 的示例输入和输出
  • 请阅读以上评论。如果没有,我可以向您解释更多。

标签: python regex string


【解决方案1】:

我会使用regex.sub() 方法和lambda 函数。

正则表达式可以轻松检测数字,然后您可以将它们替换为递增的值。

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.

如果你真的需要使用.isdigt(),你应该首先.split(),句子,然后遍历单词列表,如果元素是一个实际的int,则增加值。最后,别忘了.join() 把结果找回来。有这样的想法:

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

【讨论】:

  • 我确实想使用“isdigit()”函数来实现..在我的列表中寻找数字任务。然后将其增加 1。
  • 我明白,但我正在尝试学习“isdigit()”函数的使用...我只想通过列表找到所有数字并以 1 递增。
【解决方案2】:

@Delgan 的回答很棒,而且措辞完美。我只是想我会添加它,因为我注意到您在使用列表理解时犯了一些错误,我相信这可能是您问题的根源。

列表理解是一个很棒的功能。但是有几个部分你应该意识到。请参阅下图以查看分解的列表理解部分。

因此,当您使用另一个集合创建列表时。使用 if 语句的结尾部分用于过滤,但您实际上希望上一个列表中的所有内容(上一句中的所有单词)都在新列表中。您只想增加该句子中的所有数字。

这意味着您应该对某些元素执行功能。我在您的问题中看到list comprehensionlist 中的某些元素(特别是正整数或负整数的元素)执行功能的部分是您想要的。这应该在列表结果字段中完成。这是我的示例代码:

# 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)

哪个输出:

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

【讨论】:

  • 我注意到一个问题。这会将 -3 增加到 -4。只需将 -= 1 用于循环的那一部分,如果它是零,则可以添加特殊以从返回中删除“-”。这有点奇怪,因为 isdigit() 但你想要那个:)
【解决方案3】:

使用tryexcept 与尝试构建小型解析器相比,该示例更容易捕获错误。

>>> 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     

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    • 2023-01-09
    • 1970-01-01
    • 2021-12-30
    相关资源
    最近更新 更多