3 回答
TA贡献1934条经验 获得超2个赞
您可以按空格拆分字符串以' '列出字符串中的所有单词。然后在该列表中循环,检查给定条件的每个单词,并在必要时替换哈希。之后,您可以按空格加入列表' '以创建一个字符串并返回它。
def remove_hash(str):
words = str.split(' ') # Split the string into a list
without_hash = [] # Create a list for saving the words after removing hash
for word in words:
if re.match('^#[a-zA-Z]+', word) is not None: # check if the word starts with hash('#') and contains some characters after it.
without_hash.append(word[1:]) # it true remove the hash and append it your the ther list
else:
without_hash.append(word) # otherwise append the word as is in new list
return ' '.join(without_hash) # join the new list(without hash) by space and return it.
输出:
>>> remove_hash('# #DataScience')
'# DataScience'
>>> remove_hash('kjndjk#jnjkd')
'kjndjk#jnjkd'
>>> remove_hash("# #DataScience #KJSBDKJ kjndjk#jnjkd #jkzcjkh# iusadhuish#")
'# DataScience KJSBDKJ kjndjk#jnjkd jkzcjkh# iusadhuish#'
您可以通过避免这样的 if else 来缩短代码(但有点难以理解):
def remove_hash(str):
words = str.split(' ' )
without_hash = []
for word in words:
without_hash.append(re.sub(r'^#+(.+)', r'\1', word))
return ' '.join(without_hash)
这会给你同样的结果
TA贡献1798条经验 获得超3个赞
a = '# #DataScience'
b = 'kjndjk#jnjkd'
c = "# #DataScience #KJSBDKJ kjndjk#jnjkd #jkzcjkh# iusadhuish#"
regex = '(\s+)#(\S)'
import re
print re.sub(regex, '\\1\\2', a)
print re.sub(regex, '\\1\\2', b)
print re.sub(regex, '\\1\\2', c)
添加回答
举报