Shu*_*ami 2 python python-2.7 python-3.x
我需要从字符之前出现的部分去除字符串':'
,其中':'
可能出现多次。例如:
input: 'Mark: I am sending the file: abc.txt'
output: 'I am sending the file: abc.txt'
Run Code Online (Sandbox Code Playgroud)
我的功能是这个(Python代码)
def process_str(in_str):
str_list = in_str.split(':')[1:]
out_str = ''
for each in str_list:
out_str += each
return out_str
Run Code Online (Sandbox Code Playgroud)
我得到的输出'I am sending the file abc.txt'
没有第二个':'
. 有没有办法纠正这个问题?也可以使这段代码在时间和空间复杂度上更有效吗?
使用split()怎么样?
str = 'Mark: I am sending the file: abc.txt'
print(str.split(':', 1)[-1])
Run Code Online (Sandbox Code Playgroud)
如果分隔符不在初始字符串中,则使用 -1 来说明列表索引越界
输出:
I am sending the file: abc.txt'
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5123 次 |
最近记录: |