开发者

How do I get same result without using enumerate?

I do not need char in this example, but I include it to get my desired results.

charlist = [strval[0:count+1] for count, char in enumerate(strval)]开发者_Go百科

How do I get the same result without using enumerate?


xrange(len(strval))


If you do not want to use enumerate, use range since all you want is count value

>>> strval = "abcd"
>>> for count, char in enumerate(strval): print count, char
... 
0 a
1 b
2 c
3 d
>>> for count in range(len(strval)): print count
... 
0
1
2
3
>>>


How about replacing enumerate(...) with zip(xrange(...),...)?

[strval[0:count+1] for count, char in zip(xrange(len(strval)),strval)]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜