python3 string" abcd"打印:aababcabcd?

时间:2014-12-28 16:17:33

标签: python string

如果a有一个像abcd1234等字符串,我怎么能一起打印,第一个字符,然后是前两个字符,然后是前三个字符等?

例如对于string = 1234我想打印/返回1121231234aababcabcd

到目前为止我有这个代码:

def string_splosion(str):
    i = 0
    while i <= len(str):
        i += 1
        print(str[:i])
print(string_splosion('abcd'))

但它会在单独的行中打印/返回它。我可以手动编写print(str[0:1], str[1:2] <...>),但是我怎么做python呢,因为我不知道字符串会有多长?

3 个答案:

答案 0 :(得分:6)

您不应将str用作变量名称,因为它会隐藏built-in str type。您可以在循环中将切片的字符串连接在一起:

def string_splosion(string):
    i, result = 0, ''
    while i < len(string): # < instead of <=
        i += 1
        result += string[:i]
    return result

使用str.joinrange可以缩短您的代码:

def string_splosion(string):
    return ''.join(string[:i] for i in range(1, len(string) + 1))

或使用itertools.accumulate(Python 3.2 +):

import itertools
def string_splosion(string):
    return ''.join(itertools.accumulate(string))

itertools.accumulate方法似乎比str.join方法快2倍,比原始基于循环的解决方案快1.5倍:

string_splosion_loop(abcdef): 2.3944241080715223
string_splosion_join_gen(abcdef): 2.757582983268288
string_splosion_join_lc(abcdef): 2.2879220573578865
string_splosion_itertools(abcdef): 1.1873638161591886

我用来计算功能的代码是

import itertools
from timeit import timeit

string = 'abcdef'

def string_splosion_loop():
    i, result = 0, ''
    while i < len(string):
        i += 1
        result += string[:i]
    return result

def string_splosion_join_gen():
    return ''.join(string[:i] for i in range(1, len(string) + 1))

def string_splosion_join_lc():
    # str.join performs faster when the argument is a list
    return ''.join([string[:i] for i in range(1, len(string) + 1)])

def string_splosion_itertools():
    return ''.join(itertools.accumulate(string))

funcs = (string_splosion_loop, string_splosion_join_gen, 
         string_splosion_join_lc, string_splosion_itertools)

for f in funcs:
    print('{.__name__}({}): {}'.format(f, string, timeit(f)))

答案 1 :(得分:4)

只需使用:

"".join([s[:i] for i in range(len(s)+1)])

正如@abc所说,不要将str用作变量名,因为它是默认类型之一。见https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange

E.g:

>>> s = "1234"
>>> "".join([s[:i] for i in range(len(s)+1)])
'1121231234'
>>> s = "abcd"
>>> "".join([s[:i] for i in range(len(s)+1)])
'aababcabcd'

range(len(s)+1)是因为切片,请参阅Explain Python's slice notation

>>> s = "1234"
>>> len(s)
4
>>> range(len(s))
[0, 1, 2, 3]
>>> s[:3]
'123'
>>> range(len(s)+1)
[0, 1, 2, 3, 4]
>>> s[:4]
'1234'

然后:

>>> s[:0]
''
>>> s[:1]
'1'
>>> s[:2]
'12'
>>> s[:3]
'123'
>>> s[:4]
'1234'

最后,使用list([s[:1], s[:2], s[:3], s[:4]])加入"".join(list),请参阅https://docs.python.org/2/library/string.html#string.join

>>> list([s[:1], s[:2], s[:3], s[:4]])
['1', '12', '123', '1234']
>>> x = list([s[:1], s[:2], s[:3], s[:4]])
>>> "".join(x)
'1121231234'
>>> "-".join(x)
'1-12-123-1234'
>>> " ".join(x)
'1 12 123 1234'

为避免循环中的提取迭代,您可以使用range(1,len(s)+1),因为s[:0]返回0长度的字符串:

>>> s = "1234"
>>> "".join([s[:i] for i in range(1,len(s)+1)])
'1121231234'
>>> "".join([s[:i] for i in range(len(s)+1)])
'1121231234'

答案 2 :(得分:0)

如果你使用的是python 3,你可以使用它来打印而不需要换行符:

print(yourString, end="")

所以你的功能可能是:

def string_splosion(str):
    for i in range(len(str)):
        print(str[:i], end="")
print(string_splosion('abcd'))