Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这个:
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']
简单的:
[word[:i] for i in xrange(len(word))]