我有这个:
word = 'abcd'
我想要这个:
do_something(word)
>>['a', 'ab', 'abc', 'abcd']
答案 0 :(得分:3)
word = 'abcd'
def do_something(word):
return [word[:x + 1] for x in xrange(len(word))]
print do_something(word)
结果:
['a', 'ab', 'abc', 'abcd']
答案 1 :(得分:1)
简单:
[word[:i] for i in xrange(len(word))]