gpt4 book ai didi

python - 如何分解字符串并按顺序附加其字符?

转载 作者:行者123 更新时间:2023-11-28 22:56:39 24 4
gpt4 key购买 nike

我有这个:

word = 'abcd'

我想要这个:

do_something(word)

>>['a', 'ab', 'abc', 'abcd']

最佳答案

word = 'abcd'

def do_something(word):
return [word[:x + 1] for x in xrange(len(word))]

print do_something(word)

结果:

['a', 'ab', 'abc', 'abcd']

关于python - 如何分解字符串并按顺序附加其字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15271769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com