我经常使用这种模式:
有没有更符合Python风格的方法来实现这个?
我发现自己经常使用这种模式:
new_url = season_url.split('cid=')[0] + 'cid='
有没有更符合Python风格的方法来实现这个?
我发现自己经常使用这种模式:
new_url = season_url.split('cid=')[0] + 'cid='
像这样的内容吗?
def truncate_after(s, needle):
partition_pos = s.index(needle)+len(needle)
return s[:partition_pos]
str.partition()
,它可以创建一个长度为3的列表,其中包含分割字符之前的部分、分割字符和分割字符之后的部分。new_url = ''.join(season_url.partition('cid=')[:2])