About python while statement and pop()
hito koto
hitokoto2014 at gmail.com
Thu Jun 12 00:56:06 EDT 2014
2014年6月12日木曜日 12時58分27秒 UTC+9 Chris Angelico:
> On Thu, Jun 12, 2014 at 1:40 PM, Vincent Vande Vyvre
>
> <vincent.vandevyvre at 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]])
More information about the Python-list
mailing list