我了解到在python中有一个列表理解来操作或创建列表
比如说,
li1 = [i for i in rage(10)]
# this will create a list name with li1
# and li1 contains following:
print(li1) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
所以,这是我的问题。 如果我有下面的字符串。 有什么解决办法吗?就像列表理解一样
# The task I need to do is remove all the pucntuations from the string and replace it to empty string.
text = input() # for example: "A! Lion? is crying..,!" is given as input
punctuations = [",", ".", "!", "?"]
punc_removed_str = text.replace(p, "") for p in punctuations
# above line is what I want to do..
print(remove_punctuation)
# Then result will be like below:
# Output: A Lion is crying
谢谢大家!
没有字符串理解,但是您可以在
join()
内使用生成器表达式,该表达式用于列表理解Python在标准库中已经有一套完整的标点符号
from string import punctuation
punctuation
返回字符串!“#$%&;'()*+,-./:;<;=>;?@[]^ `{124;}~Docs
因此,您可以根据给定的输入创建一个列表,该列表检查输入中的每个字符是否在
punctuation
字符串中您可以在内置的
str.join
方法中传递结果列表相关问题 更多 >
编程相关推荐