Movatterモバイル変換


[0]ホーム

URL:


Stepping backwards in for loop?

Steve Holdensholden at holdenweb.com
Sun Apr 15 11:28:12 EDT 2001


"Andrew Dalke" <dalke at acm.org> wrote in messagenews:9banud$ge$1 at slb6.atl.mindspring.net...> Carlos Ribeiro wrote:> >It would be a *lot* easier if strings had a reverse method, or if the> >reverse() methods returned the reversed string. However, similarly to> >sort(), the Python-way-of-doing-things must have some good reason for> >reverse() to behave this way (as a inplace operation on the list).>> Lists have a reverse method because lists are mutable.> Consider strings as more akin to tuples.  Both are immutable.>But there's no good reason why there shouldn't be a "reverse" function whichreturns the reverse of any sequence, surely? The question then becomeswhether a built-in function could improve on the efficiency of>>> def sreverse(s):...  l = list(s)...  l.reverse()...  return "".join(l)...>>> sreverse("abcdefg")'gfedcba'I'd be very surprised if it couldn't ... though then there's Unicode to takeinto account.regards STeve


More information about the Python-listmailing list

[8]ページ先頭

©2009-2025 Movatter.jp