Movatterモバイル変換


[0]ホーム

URL:


Stepping backwards in for loop?

Alex Martellialeaxit at yahoo.com
Sat Apr 14 10:30:36 EDT 2001


"Gustaf Liljegren" <gustafl at algonet.se> wrote in messagenews:Xns9083A46CB3B93gustaflalgonetse at 194.213.69.152...> Can't figure out how to step backwards, character by character in astring.> I was looking for a loop countruct with a counter to handle this, but the> for loop in Python doesn't let me step backwards. What is the alternative?You have several alternatives, such as:thestr = 'ciao'for x in range(len(thestr), 0, -1):    print thestr[x-1],printfor x in range(len(thestr)):    print thestr[-x-1],printaulist = list(thestr)aulist.reverse()for c in aulist:    print c,printclass Reverse:    def __init__(self, seq):        self.seq = seq    def __getitem__(self, index):        return self.seq[-index-1]for c in Reverse('ciao'):    print c,printEach of these snippets will print 'o a i c'.Alex


More information about the Python-listmailing list

[8]ページ先頭

©2009-2025 Movatter.jp