简体   繁体   中英

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)]

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
>>>

如何用zip(xrange(...),...)替换enumerate(...) zip(xrange(...),...)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM