Re: About python while statement and pop()
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]] ) -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
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]] ) -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
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! -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
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) [] -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
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] -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
On Fri, Jun 13, 2014 at 2:49 AM, Mark H Harris harrismh...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
while done==False: Correction: while not done: Better Python and not bad English, either. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
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] -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
On 6/12/14 11:57 AM, Chris Angelico wrote: On Fri, Jun 13, 2014 at 2:49 AM, Mark H Harris harrismh...@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? ;-) -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
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] == ah === -- https://mail.python.org/mailman/listinfo/python-list
About python while statement and pop()
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
On Thu, Jun 12, 2014 at 1:40 PM, Vincent Vande Vyvre vincent.vandevy...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
2014年6月12日木曜日 12時58分27秒 UTC+9 Chris Angelico: On Thu, Jun 12, 2014 at 1:40 PM, Vincent Vande Vyvre vincent.vandevy...@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]]) -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
On Thu, Jun 12, 2014 at 2:56 PM, hito koto hitokoto2...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
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 -- https://mail.python.org/mailman/listinfo/python-list