小编Dev*_*oki的帖子

Python 是否有像列表理解那样的字符串理解?

Python 是否有一个类似于列表理解的结构来创建字符串,即“字符串理解”?

我需要做的任务是删除字符串中的所有标点符号。

def remove_punctuation(text: str) -> str:
    punctuations = [",", ".", "!", "?"]
    # next line is what I want to do
    result = text.replace(p, "") for p in punctuations
    return result

assert remove_punctuation("A! Lion?is. crying!..") == "A Lion is crying"
Run Code Online (Sandbox Code Playgroud)

python string list-comprehension

7
推荐指数
1
解决办法
4460
查看次数

标签 统计

list-comprehension ×1

python ×1

string ×1