Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73196 > unrolled thread
| Started by | hito koto <hitokoto2014@gmail.com> |
|---|---|
| First post | 2014-06-11 20:12 -0700 |
| Last post | 2014-06-12 11:37 -0700 |
| Articles | 18 — 8 participants |
Back to article view | Back to comp.lang.python
About python while statement and pop() hito koto <hitokoto2014@gmail.com> - 2014-06-11 20:12 -0700
Re:About python while statement and pop() Dave Angel <davea@davea.name> - 2014-06-11 22:34 -0500
Re: About python while statement and pop() Vincent Vande Vyvre <vincent.vandevyvre@swing.be> - 2014-06-12 05:40 +0200
Re: About python while statement and pop() Chris Angelico <rosuav@gmail.com> - 2014-06-12 13:58 +1000
Re: About python while statement and pop() hito koto <hitokoto2014@gmail.com> - 2014-06-11 21:56 -0700
Re: About python while statement and pop() Chris Angelico <rosuav@gmail.com> - 2014-06-12 15:08 +1000
Re: About python while statement and pop() Steven D'Aprano <steve@pearwood.info> - 2014-06-12 05:43 +0000
Re: About python while statement and pop() hito koto <hitokoto2014@gmail.com> - 2014-06-11 23:06 -0700
Re: About python while statement and pop() hito koto <hitokoto2014@gmail.com> - 2014-06-12 00:13 -0700
Re: About python while statement and pop() hito koto <hitokoto2014@gmail.com> - 2014-06-12 02:51 -0700
Re: About python while statement and pop() Mark H Harris <harrismh777@gmail.com> - 2014-06-12 11:41 -0500
Re: About python while statement and pop() Mark H Harris <harrismh777@gmail.com> - 2014-06-12 11:49 -0500
Re: About python while statement and pop() Marko Rauhamaa <marko@pacujo.net> - 2014-06-12 19:55 +0300
Re: About python while statement and pop() Mark H Harris <harrismh777@gmail.com> - 2014-06-12 12:07 -0500
Re: About python while statement and pop() Chris Angelico <rosuav@gmail.com> - 2014-06-13 02:57 +1000
Re: About python while statement and pop() Mark H Harris <harrismh777@gmail.com> - 2014-06-12 12:10 -0500
Re: About python while statement and pop() Mark H Harris <harrismh777@gmail.com> - 2014-06-12 12:16 -0500
Re: About python while statement and pop() wxjmfauth@gmail.com - 2014-06-12 11:37 -0700
| From | hito koto <hitokoto2014@gmail.com> |
|---|---|
| Date | 2014-06-11 20:12 -0700 |
| Subject | About python while statement and pop() |
| Message-ID | <8d6207ab-b883-4940-8e53-75546a91d4dd@googlegroups.com> |
Hello,all
I'm first time,
I want to make a while statement which can function the same x.pop () and without the use of pop、how can i to do?
i want to change this is code:
def foo(x):
y = []
while x !=[]:
y.append(x.pop())
return y
[toc] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-06-11 22:34 -0500 |
| Message-ID | <mailman.11024.1402544026.18130.python-list@python.org> |
| In reply to | #73196 |
hito koto <hitokoto2014@gmail.com> Wrote in message: > Hello,all > I'm first time, > > I want to make a while statement which can function the same x.pop () and without the use of pop、how can i to do? No idea what the question means. Are you just trying to rewrite the loop in a python implementation where pop is broken? > > i want to change this is code: > > def foo(x): > y = [] > while x !=[]: > y.append(x.pop()) > return y Perhaps you're looking for the extend method. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Vincent Vande Vyvre <vincent.vandevyvre@swing.be> |
|---|---|
| Date | 2014-06-12 05:40 +0200 |
| Message-ID | <mailman.11025.1402544865.18130.python-list@python.org> |
| In reply to | #73196 |
Le 12/06/2014 05:12, hito koto a écrit :
> Hello,all
> I'm first time,
>
> I want to make a while statement which can function the same x.pop () and without the use of pop、how can i to do?
>
> i want to change this is code:
>
> def foo(x):
> y = []
> while x !=[]:
> y.append(x.pop())
> return y
Something like that :
def foo(x):
return reversed(x)
--
Vincent V.V.
Oqapy <https://launchpad.net/oqapy> . Qarte
<https://launchpad.net/qarte> . PaQager <https://launchpad.net/paqager>
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-06-12 13:58 +1000 |
| Message-ID | <mailman.11026.1402545515.18130.python-list@python.org> |
| In reply to | #73196 |
On Thu, Jun 12, 2014 at 1:40 PM, Vincent Vande Vyvre
<vincent.vandevyvre@swing.be> wrote:
> Le 12/06/2014 05:12, hito koto a écrit :
>
>> Hello,all
>> I'm first time,
>>
>> I want to make a while statement which can function the same x.pop () and
>> without the use of pop、how can i to do?
>>
>> i want to change this is code:
>>
>> def foo(x):
>> y = []
>> while x !=[]:
>> y.append(x.pop())
>> return y
>
> Something like that :
>
> def foo(x):
> return reversed(x)
That doesn't do the same thing, though. Given a list x, the original
function will empty that list and return a new list in reverse order,
but yours will return a reversed iterator over the original list
without changing it. This is more accurate, but still not identical,
and probably not what the OP's teacher is looking for:
def foo(x):
y = x[::-1]
x[:] = []
return y
If the mutation of x is unimportant, it can simply be:
def foo(x):
return x[::-1]
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | hito koto <hitokoto2014@gmail.com> |
|---|---|
| Date | 2014-06-11 21:56 -0700 |
| Message-ID | <814a6cb2-6e62-4c21-acca-71e30b22ec9f@googlegroups.com> |
| In reply to | #73199 |
2014年6月12日木曜日 12時58分27秒 UTC+9 Chris Angelico: > On Thu, Jun 12, 2014 at 1:40 PM, Vincent Vande Vyvre > > <vincent.vandevyvre@swing.be> wrote: > > > Le 12/06/2014 05:12, hito koto a écrit : > > > > > >> Hello,all > > >> I'm first time, > > >> > > >> I want to make a while statement which can function the same x.pop () and > > >> without the use of pop、how can i to do? > > >> > > >> i want to change this is code: > > >> > > >> def foo(x): > > >> y = [] > > >> while x !=[]: > > >> y.append(x.pop()) > > >> return y > > > > > > Something like that : > > > > > > def foo(x): > > > return reversed(x) > > > > That doesn't do the same thing, though. Given a list x, the original > > function will empty that list and return a new list in reverse order, > > but yours will return a reversed iterator over the original list > > without changing it. This is more accurate, but still not identical, > > and probably not what the OP's teacher is looking for: > > > > def foo(x): > > y = x[::-1] > > x[:] = [] > > return y > > > > If the mutation of x is unimportant, it can simply be: > > > > def foo(x): > > return x[::-1] > > > > ChrisA I want to use while statement, for example: >>> def foo(x): ... y = [] ... while x !=[]: ... y.append(x.pop()) ... return y ... >>> print foo(a) [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] >>> a [] but this is empty >>> so,I want to leave a number of previous (a = [[1, 2, 3, 4],[5, 6, 7, 8, 9],[10]])
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-06-12 15:08 +1000 |
| Message-ID | <mailman.11030.1402549704.18130.python-list@python.org> |
| In reply to | #73203 |
On Thu, Jun 12, 2014 at 2:56 PM, hito koto <hitokoto2014@gmail.com> wrote: > I want to use while statement, This sounds like homework. Go back to your teacher/tutor for assistance, rather than asking us to do the work for you; or at very least, word your question in such a way that we can help you to learn, rather than just give you the answer. Second problem: You're using Google Groups. This makes your posts messy, especially when you quote someone else's text. Please either fix your posts before sending them, or read and post by some other means, such as the mailing list: https://mail.python.org/mailman/listinfo/python-list ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2014-06-12 05:43 +0000 |
| Message-ID | <53993e0e$0$11121$c3e8da3@news.astraweb.com> |
| In reply to | #73203 |
On Wed, 11 Jun 2014 21:56:06 -0700, hito koto wrote:
> I want to use while statement,
>
> for example:
>>>> def foo(x):
> ... y = []
> ... while x !=[]:
> ... y.append(x.pop())
> ... return y
> ...
>>>> print foo(a)
> [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]]
>>>> a
> [] but this is empty
>>>> so,I want to leave a number of previous (a = [[1, 2, 3, 4],[5, 6, 7,
>>>> 8, 9],[10]])
I wouldn't use a while statement. The easy way is:
py> a = [[1, 2, 3, 4],[5, 6, 7, 8, 9],[10]]
py> y = a[::-1]
py> print y
[[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]]
py> print a
[[1, 2, 3, 4], [5, 6, 7, 8, 9], [10]]
If you MUST use a while loop, then you need something like this:
def foo(x):
y = []
index = 0
while index < len(x):
y.append(x[i])
i += 1
return y
This does not copy in reverse order. To make it copy in reverse order,
index should start at len(x) - 1 and end at 0.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | hito koto <hitokoto2014@gmail.com> |
|---|---|
| Date | 2014-06-11 23:06 -0700 |
| Message-ID | <71d2fe9f-2b44-4ef3-82b8-07a322d638b3@googlegroups.com> |
| In reply to | #73205 |
2014年6月12日木曜日 14時43分42秒 UTC+9 Steven D'Aprano: > On Wed, 11 Jun 2014 21:56:06 -0700, hito koto wrote: > > > > > I want to use while statement, > > > > > > for example: > > >>>> def foo(x): > > > ... y = [] > > > ... while x !=[]: > > > ... y.append(x.pop()) > > > ... return y > > > ... > > >>>> print foo(a) > > > [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] > > >>>> a > > > [] but this is empty > > >>>> so,I want to leave a number of previous (a = [[1, 2, 3, 4],[5, 6, 7, > > >>>> 8, 9],[10]]) > > > > > > I wouldn't use a while statement. The easy way is: > > > > py> a = [[1, 2, 3, 4],[5, 6, 7, 8, 9],[10]] > > py> y = a[::-1] > > py> print y > > [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] > > py> print a > > [[1, 2, 3, 4], [5, 6, 7, 8, 9], [10]] > > > > If you MUST use a while loop, then you need something like this: > > > > > > def foo(x): > > y = [] > > index = 0 > > while index < len(x): > > y.append(x[i]) > > i += 1 > > return y > > > > > > This does not copy in reverse order. To make it copy in reverse order, > > index should start at len(x) - 1 and end at 0. > > > > > > > > -- > > Steven Hi, Steven: Thanks, My goal is to be able to in many ways python Sorry, I was mistake, I want to leave a number of previous (a = [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] )
[toc] | [prev] | [next] | [standalone]
| From | hito koto <hitokoto2014@gmail.com> |
|---|---|
| Date | 2014-06-12 00:13 -0700 |
| Message-ID | <cb941262-2271-4f4c-9f7e-63fe3cd14fca@googlegroups.com> |
| In reply to | #73205 |
2014年6月12日木曜日 14時43分42秒 UTC+9 Steven D'Aprano: > On Wed, 11 Jun 2014 21:56:06 -0700, hito koto wrote: > > > > > I want to use while statement, > > > > > > for example: > > >>>> def foo(x): > > > ... y = [] > > > ... while x !=[]: > > > ... y.append(x.pop()) > > > ... return y > > > ... > > >>>> print foo(a) > > > [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] > > >>>> a > > > [] but this is empty > > >>>> so,I want to leave a number of previous (a = [[1, 2, 3, 4],[5, 6, 7, > > >>>> 8, 9],[10]]) > > > > > > I wouldn't use a while statement. The easy way is: > > > > py> a = [[1, 2, 3, 4],[5, 6, 7, 8, 9],[10]] > > py> y = a[::-1] > > py> print y > > [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] > > py> print a > > [[1, 2, 3, 4], [5, 6, 7, 8, 9], [10]] > > > > If you MUST use a while loop, then you need something like this: > > > > > > def foo(x): > > y = [] > > index = 0 > > while index < len(x): > > y.append(x[i]) > > i += 1 > > return y > > > > > > This does not copy in reverse order. To make it copy in reverse order, > > index should start at len(x) - 1 and end at 0. > > > > > > > > -- > > Steven Hi, Steven: Thanks, My goal is to be able to in many ways python Sorry, I was mistake, I want to leave a number of previous (a = [[10], [9, 8, 7, 6, 5], [4, 3, 2, 1]] )
[toc] | [prev] | [next] | [standalone]
| From | hito koto <hitokoto2014@gmail.com> |
|---|---|
| Date | 2014-06-12 02:51 -0700 |
| Message-ID | <e7d7be9f-d0f7-412d-ba0e-b7bde2237ca7@googlegroups.com> |
| In reply to | #73205 |
2014年6月12日木曜日 14時43分42秒 UTC+9 Steven D'Aprano: > On Wed, 11 Jun 2014 21:56:06 -0700, hito koto wrote: > > > > > I want to use while statement, > > > > > > for example: > > >>>> def foo(x): > > > ... y = [] > > > ... while x !=[]: > > > ... y.append(x.pop()) > > > ... return y > > > ... > > >>>> print foo(a) > > > [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] > > >>>> a > > > [] but this is empty > > >>>> so,I want to leave a number of previous (a = [[1, 2, 3, 4],[5, 6, 7, > > >>>> 8, 9],[10]]) > > > > > > I wouldn't use a while statement. The easy way is: > > > > py> a = [[1, 2, 3, 4],[5, 6, 7, 8, 9],[10]] > > py> y = a[::-1] > > py> print y > > [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] > > py> print a > > [[1, 2, 3, 4], [5, 6, 7, 8, 9], [10]] > > > > If you MUST use a while loop, then you need something like this: > > > > > > def foo(x): > > y = [] > > index = 0 > > while index < len(x): > > y.append(x[i]) > > i += 1 > > return y > > > > > > This does not copy in reverse order. To make it copy in reverse order, > > index should start at len(x) - 1 and end at 0. > > > > > > > > -- > > Steven Hi, Thank you!
[toc] | [prev] | [next] | [standalone]
| From | Mark H Harris <harrismh777@gmail.com> |
|---|---|
| Date | 2014-06-12 11:41 -0500 |
| Message-ID | <lncl7g$ni6$1@speranza.aioe.org> |
| In reply to | #73196 |
On 6/11/14 10:12 PM, hito koto wrote: > i want to change this is code: > > def foo(x): > y = [] > while x !=[]: > y.append(x.pop()) > return y > Consider this generator (all kinds of permutations on the idea): >>> L1 [1, 2, 3, 4, 5, 6, 7] >>> def poplist(L): while True: yield L[::-1][:1:] L = L[::-1][1::][::-1] >>> pop = poplist(L1) >>> next(pop) [7] >>> next(pop) [6] >>> next(pop) [5] >>> next(pop) [4] >>> next(pop) [3] >>> next(pop) [2] >>> next(pop) [1] >>> next(pop) [] >>> next(pop) []
[toc] | [prev] | [next] | [standalone]
| From | Mark H Harris <harrismh777@gmail.com> |
|---|---|
| Date | 2014-06-12 11:49 -0500 |
| Message-ID | <lnclm5$oe4$1@speranza.aioe.org> |
| In reply to | #73196 |
On 6/11/14 10:12 PM, hito koto wrote: > def foo(x): > y = [] > while x !=[]: > y.append(x.pop()) > return y > Consider this generator variation: >>> def poplist(L): done = False while done==False: yield L[::-1][:1:] L = L[::-1][1::][::-1] if len(L)==0: done=True >>> L1=[1, 2, 3, 4, 5, 6, 7] >>> for n in poplist(L1): print(n) [7] [6] [5] [4] [3] [2] [1] >>>
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2014-06-12 19:55 +0300 |
| Message-ID | <87ppievzhn.fsf@elektro.pacujo.net> |
| In reply to | #73221 |
> while done==False: Correction: while not done: Better Python and not bad English, either. Marko
[toc] | [prev] | [next] | [standalone]
| From | Mark H Harris <harrismh777@gmail.com> |
|---|---|
| Date | 2014-06-12 12:07 -0500 |
| Message-ID | <lncmnj$rho$1@speranza.aioe.org> |
| In reply to | #73222 |
On 6/12/14 11:55 AM, Marko Rauhamaa wrote: > while not done: > > Better Python and not bad English, either. ... and taking Marko's good advice, what I think you really wanted: >>> def poplist(L): done = False while not done: yield L[::-1][:1:] L = L[::-1][1::][::-1] if len(L)==0: done=True >>> L=[1, 2, 3, 4, 5, 6, 7] >>> m=[] >>> pop = poplist(L) >>> for n in poplist(L): m.append(n[0]) >>> m [7, 6, 5, 4, 3, 2, 1] >>>
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-06-13 02:57 +1000 |
| Message-ID | <mailman.11035.1402592234.18130.python-list@python.org> |
| In reply to | #73221 |
On Fri, Jun 13, 2014 at 2:49 AM, Mark H Harris <harrismh777@gmail.com> wrote: > Consider this generator variation: > >>>> def poplist(L): > done = False > while done==False: > > yield L[::-1][:1:] > L = L[::-1][1::][::-1] > if len(L)==0: done=True Why not just "while L"? Or are you deliberately trying to ensure that cheating will be detectable? ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Mark H Harris <harrismh777@gmail.com> |
|---|---|
| Date | 2014-06-12 12:10 -0500 |
| Message-ID | <lncmur$s9h$1@speranza.aioe.org> |
| In reply to | #73223 |
On 6/12/14 11:57 AM, Chris Angelico wrote: > On Fri, Jun 13, 2014 at 2:49 AM, Mark H Harris <harrismh777@gmail.com> wrote: >> Consider this generator variation: >> >>>>> def poplist(L): >> done = False >> while done==False: >> >> yield L[::-1][:1:] >> L = L[::-1][1::][::-1] >> if len(L)==0: done=True > > Why not just "while L"? Or are you deliberately trying to ensure that > cheating will be detectable? ;-)
[toc] | [prev] | [next] | [standalone]
| From | Mark H Harris <harrismh777@gmail.com> |
|---|---|
| Date | 2014-06-12 12:16 -0500 |
| Message-ID | <lncn90$t1m$1@speranza.aioe.org> |
| In reply to | #73223 |
On 6/12/14 11:57 AM, Chris Angelico wrote: >>>>> def poplist(L): >> done = False >> while done==False: >> >> yield L[::-1][:1:] >> L = L[::-1][1::][::-1] >> if len(L)==0: done=True > > Why not just "while L"? OK, here it is with Chris' excellent advice: >>> def poplist(L): while L: yield L[::-1][:1:] L = L[::-1][1::][::-1] >>> L=[1, 2, 3, 4, 5, 6, 7] >>> m=[] >>> pop = poplist(L) >>> for n in poplist(L): m.append(n[0]) >>> m [7, 6, 5, 4, 3, 2, 1] >>> ========== aaaaah ===================
[toc] | [prev] | [next] | [standalone]
| From | wxjmfauth@gmail.com |
|---|---|
| Date | 2014-06-12 11:37 -0700 |
| Message-ID | <721c8c8e-3512-42cc-be19-ede489cef7d4@googlegroups.com> |
| In reply to | #73228 |
Le jeudi 12 juin 2014 19:16:19 UTC+2, Mark H. Harris a écrit : > On 6/12/14 11:57 AM, Chris Angelico wrote: > > >>>>> def poplist(L): > > >> done = False > > >> while done==False: > > >> > > >> yield L[::-1][:1:] > > >> L = L[::-1][1::][::-1] > > >> if len(L)==0: done=True > > > > > > Why not just "while L"? > > > > OK, here it is with Chris' excellent advice: > > > > >>> def poplist(L): > > while L: > > yield L[::-1][:1:] > > L = L[::-1][1::][::-1] > > > > > > >>> L=[1, 2, 3, 4, 5, 6, 7] > > >>> m=[] > > >>> pop = poplist(L) > > >>> for n in poplist(L): > > m.append(n[0]) > > > > > > >>> m > > [7, 6, 5, 4, 3, 2, 1] > > >>> > > > > > > ========== aaaaah =================== >>> a = [1, 2, 3, 4, 5, 6, 7] >>> b = a[:] # si nécessaire, wenn nötig >>> b.reverse() >>> >>> a [1, 2, 3, 4, 5, 6, 7] >>> b [7, 6, 5, 4, 3, 2, 1] jmf
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web