给定标题和 max_length,缩短标题的最佳方法是什么?以下是我的想法:
def shorten_title(title, max_length):
if len(title) > max_length:
split_title = title.split()
length = len(title)
n = 1
while length > max_length:
shortened_title = split_title[:length(split_title)-n]
n+=1
return shortened_title
else:
return title