最简单的python相当于R的gsub

[英]simplest python equivalent to R's gsub


Is there a simple/one-line python equivalent to R's gsub function?

有一个简单/单行python等同于R的gsub函数吗?

strings = c("Important text,      !Comment that could be removed", "Other String")
gsub("(,[ ]*!.*)$", "", strings) 
# [1] "Important text" "Other String"  

1 个解决方案

#1


10  

For a a string:

对于一个字符串:

import re
string = "Important text,      !Comment that could be removed"
re.sub("(,[ ]*!.*)$", "", string)

Since you updated your question to be a list of strings, you can use a list comprehension.

由于您将问题更新为字符串列表,因此可以使用列表推导。

import re
strings = ["Important text,      !Comment that could be removed", "Other String"]
[re.sub("(,[ ]*!.*)$", "", x) for x in strings]

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2016/08/04/48814ccbdc4346cccf1b859669ad5e1f.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号