【问题标题】:if- else statement in one line code python; syntax error一行代码python中的if-else语句;语法错误
【发布时间】:2019-09-25 10:18:17
【问题描述】:

我只想转换文本中的数字,如果是文本,则返回单词,但在 else 之后出现语法错误。语句可以写成一行吗?

text = [p.number_to_words(words) for words in text if words.isdigit() else words]

【问题讨论】:

  • 以后在询问有关错误的问题时,请始终将错误完整完整地以文本形式复制粘贴到问题中。

标签: python if-statement syntax


【解决方案1】:

只需将if ... else 移到for ... in 之前:

text = [p.number_to_words(words) if words.isdigit() else words for words in text]

【讨论】:

    猜你喜欢
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多