Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将单词分成不同的递增字母组合_Python_List - Fatal编程技术网

Python 如何将单词分成不同的递增字母组合

Python 如何将单词分成不同的递增字母组合,python,list,Python,List,我想在一个列表中分割一个单词,并将其打印成单独的部分。 因此,如果我的单词是粉色和hello,那么我如何才能得到结果 word = [pink] return [p, pi, pin, pink] word2 = [hello] return [h, he, hel, hell, hello] 你想要的是一个词的不同前缀 def prefixes(word): return [word[:i + 1] for i in range(len(word))] print(prefix

我想在一个列表中分割一个单词,并将其打印成单独的部分。 因此,如果我的单词是粉色和hello,那么我如何才能得到结果

word = [pink]
return [p,  pi, pin, pink]

word2 = [hello]
return [h, he, hel, hell, hello]

你想要的是一个词的不同前缀

def prefixes(word):
    return [word[:i + 1] for i in range(len(word))]

print(prefixes("hello"))  # ['h', 'he', 'hel', 'hell', 'hello']
print(prefixes("pink"))   # ['p', 'pi', 'pin', 'pink']

你尝试了什么?你能告诉我们你到目前为止尝试了什么吗?嗨,既然你现在有了答案,你可以考虑奖励给你最有帮助的评论的人。