在 Python 中获取字符串的所有后缀(后缀列表)

问题:

给定一个字符串,例如 foobar,你想获取该字符串的所有后缀列表,例如 ["r", "ar", "bar", "obar", "oobar", "foobar"]

解决方案

使用此代码片段:

all_suffixes.py
def all_suffixes(s):
    return [s[-i:] for i in range(1, len(s) + 1)]

s = "foobar"
print(all_suffixes(s)) # ['r', 'ar', 'bar', 'obar', 'oobar', 'foobar']

Check out similar posts by category: Algorithms, Python