Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91588 > unrolled thread
| Started by | fl <rxjwg98@gmail.com> |
|---|---|
| First post | 2015-05-31 12:40 -0700 |
| Last post | 2015-06-01 09:11 +0300 |
| Articles | 10 — 7 participants |
Back to article view | Back to comp.lang.python
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
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-05-31 12:40 -0700 |
| Subject | What use for reversed()? |
| Message-ID | <514c05fc-dded-4278-ac86-3e8e3cd0e851@googlegroups.com> |
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. Thanks,
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-05-31 19:58 +0000 |
| Message-ID | <mkfp4q$lkf$3@dont-email.me> |
| In reply to | #91588 |
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
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-05-31 18:06 -0600 |
| Message-ID | <mailman.254.1433121593.5151.python-list@python.org> |
| In reply to | #91590 |
On Sun, May 31, 2015 at 1:58 PM, Denis McMahon <denismfmcmahon@gmail.com> wrote:
> 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
The str.join method will happily accept an iterator, so the
intermediate list construction in those examples is unnecessary.
[toc] | [prev] | [next] | [standalone]
| From | cheshire <supportNOSPAM@microsoft.com> |
|---|---|
| Date | 2015-05-31 23:26 +0200 |
| Message-ID | <1e45fkwb94gzo$.1rtdbc942jqny.dlg@40tude.net> |
| In reply to | #91588 |
On Sun, 31 May 2015 12:40:19 -0700 (PDT), fl wrote: > 2. If reversed() is wrong the my purpose, what method can do it? i.e. '4321' > out. Try using slices: >>> b='1234' >>> b[::-1] '4321' https://docs.python.org/2/whatsnew/2.3.html#extended-slices
[toc] | [prev] | [next] | [standalone]
| From | Tim Delaney <timothy.c.delaney@gmail.com> |
|---|---|
| Date | 2015-06-01 09:23 +1000 |
| Message-ID | <mailman.252.1433114589.5151.python-list@python.org> |
| In reply to | #91588 |
[Multipart message — attachments visible in raw view] — view raw
On 1 June 2015 at 05:40, fl <rxjwg98@gmail.com> 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
>
Any time you get a SyntaxError, it means that you have coded something
which does not match the specified syntax of the language version.
Assuming you copied and pasted the above, I can see an error:
for br in b
The for statement must have a colon at the end of line e.g. a complete for
statement and block is:
for br in b:
print br
This will output the characters one per line (on Python 3.x), since that is
what the reversed() iterator will return. You will need to do something
else to get it back to a single string.
Have you read through the python tutorials?
https://docs.python.org/3/tutorial/
or for Python 2.x:
https://docs.python.org/2/tutorial/
Tim Delaney
[toc] | [prev] | [next] | [standalone]
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-05-31 20:11 -0700 |
| Message-ID | <1e2831cb-9eff-4912-8c89-fc642b47c3ff@googlegroups.com> |
| In reply to | #91599 |
On Sunday, May 31, 2015 at 4:23:19 PM UTC-7, Tim Delaney wrote: > On 1 June 2015 at 05:40, fl <rxj...@gmail.com> wrote: > Hi, > > The for statement must have a colon at the end of line e.g. a complete for statement and block is: > > for br in b: > print br > > This will output the characters one per line (on Python 3.x), since that is what the reversed() iterator will return. You will need to do something else to get it back to a single string. > > > Have you read through the python tutorials? > > https://docs.python.org/3/tutorial/ > > > or for Python 2.x: > > https://docs.python.org/2/tutorial/ > > Tim Delaney Thank all of you. This is the third time I learn Python. Even though I had learnt two times, I haven't grasp it. I hope that I can gain a big jump now. I had read the help tutorial, but forgot it since the long time. But your reminding does make me remember these stuff.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-06-01 01:30 +0100 |
| Message-ID | <mailman.255.1433121593.5151.python-list@python.org> |
| In reply to | #91588 |
On 01/06/2015 00:23, Tim Delaney wrote: > On 1 June 2015 at 05:40, fl <rxjwg98@gmail.com > <mailto:rxjwg98@gmail.com>> 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 > > > Any time you get a SyntaxError, it means that you have coded something > which does not match the specified syntax of the language version. > > Assuming you copied and pasted the above, I can see an error: > > for br in b > > The for statement must have a colon at the end of line e.g. a complete > for statement and block is: > > for br in b: > print br > > This will output the characters one per line (on Python 3.x), since that > is what the reversed() iterator will return. You will need to do > something else to get it back to a single string. Will it indeed? Perhaps fixing the syntax error will get something to print :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Tim Delaney <timothy.c.delaney@gmail.com> |
|---|---|
| Date | 2015-06-01 13:59 +1000 |
| Message-ID | <mailman.263.1433133429.5151.python-list@python.org> |
| In reply to | #91588 |
[Multipart message — attachments visible in raw view] — view raw
On 1 June 2015 at 10:30, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> On 01/06/2015 00:23, Tim Delaney wrote:
>
>> The for statement must have a colon at the end of line e.g. a complete
>> for statement and block is:
>>
>> for br in b:
>> print br
>>
>> This will output the characters one per line (on Python 3.x), since that
>> is what the reversed() iterator will return. You will need to do
>> something else to get it back to a single string.
>>
>
> Will it indeed? Perhaps fixing the syntax error will get something to
> print :)
Indeed - as Mark is so gently alluding to, I've done the reverse of what I
said - given Python 2.x syntax instead of Python 3.x.
That should have been:
for br in b:
print(br)
Tim Delaney
[toc] | [prev] | [next] | [standalone]
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-05-31 22:15 -0700 |
| Message-ID | <b1d480da-13e2-4c03-b989-54245b092fdf@googlegroups.com> |
| In reply to | #91588 |
On Sunday, May 31, 2015 at 12:59:47 PM UTC-7, Denis McMahon wrote:
> On Sun, 31 May 2015 12:40:19 -0700, fl wrote:
> 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,
I follow your reply with these trials:
>>>>list_r=(reversed("fred"))
>>> list(list_r)
['d', 'e', 'r', 'f']
>>> list_r
<reversed object at 0x02B57F10>
I have searched about list, but I still don't know what list_r is.
It looks like an index in other language.
What else can it be used besides list(list_r)?
I want to show list_r content. This is possibly an illegal question.
Thanks,
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2015-06-01 09:11 +0300 |
| Message-ID | <lf5bnh0ot4t.fsf@ling.helsinki.fi> |
| In reply to | #91619 |
fl writes:
> On Sunday, May 31, 2015 at 12:59:47 PM UTC-7, Denis McMahon wrote:
>> On Sun, 31 May 2015 12:40:19 -0700, fl wrote:
>> 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:
[snip]
> I follow your reply with these trials:
>
> >>>>list_r=(reversed("fred"))
> >>> list(list_r)
> ['d', 'e', 'r', 'f']
> >>> list_r
> <reversed object at 0x02B57F10>
>
> I have searched about list, but I still don't know what list_r is.
It's an object that will produce elements on demand. The doc string
(Python 2.7.6) calls it an iterator: "reversed(sequence) -> reverse
iterator over values of the sequence".
Such objects "have state". Try to consume it *twice*, it'll be empty the
second time:
>>> listr = reversed('fred')
>>> listr
<reversed object at 0xb70abc2c>
>>> list(listr)
['d', 'e', 'r', 'f']
>>> listr
<reversed object at 0xb70abc2c>
>>> list(listr)
[]
You can ask for the next element of the iterator:
>>> listr = reversed('fred')
>>> next(listr)
'd'
>>> list(listr)
['e', 'r', 'f']
> What else can it be used besides list(list_r)?
Piecemeal walking using next(list_r), as the source of elements in a for
loop, as a sequence argument to many functions that only need to walk it
once (but they will consume it!).
The suggested ''.join(reversed('fred')) is an example.
> I want to show list_r content. This is possibly an illegal question.
You could do this:
>>> from __future__ import print_function
>>> listr = reversed('fred')
>>> for c in listr: print(c, end = '')
... else: print()
...
derf
But then listr won't have that content, or any content, any more. The
act of looking inside has changed listr :)
Such objects are quite powerful when used properly. The sequence they
produce can be larger than available memory because it need only exist
one element at a time.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web