Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91590
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: What use for reversed()? |
| Date | 2015-05-31 19:58 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <mkfp4q$lkf$3@dont-email.me> (permalink) |
| References | <514c05fc-dded-4278-ac86-3e8e3cd0e851@googlegroups.com> |
On Sun, 31 May 2015 12:40:19 -0700, fl wrote:
> Hi,
>
> I have a string b='1234'. I run: br=reversed(b)
>
> I hope that I can print out '4321' by:
>
> for br in b
>
> but it complains:
> SyntaxError: invalid syntax
>
>
> My questions:
> 1. What use for reversed(). I do not find an example on web.
>
> 2. If reversed() is wrong the my purpose, what method can do it? i.e.
> '4321'
> out.
reversed returns an iterator, not a list, so it returns the reversed list
of elements one at a time. You can use list() or create a list from
reversed and then join the result:
$ python
Python 2.7.3 (default, Dec 18 2014, 19:10:20)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> "".join(list(reversed("fred")))
'derf'
>>> "".join([x for x in reversed("fred")])
'derf'
So reversed can do it, but needs a little help
--
Denis McMahon, denismfmcmahon@gmail.com
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What use for reversed()? fl <rxjwg98@gmail.com> - 2015-05-31 12:40 -0700
Re: What use for reversed()? Denis McMahon <denismfmcmahon@gmail.com> - 2015-05-31 19:58 +0000
Re: What use for reversed()? Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-31 18:06 -0600
Re: What use for reversed()? cheshire <supportNOSPAM@microsoft.com> - 2015-05-31 23:26 +0200
Re: What use for reversed()? Tim Delaney <timothy.c.delaney@gmail.com> - 2015-06-01 09:23 +1000
Re: What use for reversed()? fl <rxjwg98@gmail.com> - 2015-05-31 20:11 -0700
Re: What use for reversed()? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-01 01:30 +0100
Re: What use for reversed()? Tim Delaney <timothy.c.delaney@gmail.com> - 2015-06-01 13:59 +1000
Re: What use for reversed()? fl <rxjwg98@gmail.com> - 2015-05-31 22:15 -0700
Re: What use for reversed()? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-06-01 09:11 +0300
csiph-web