About python while statement and pop()
hito koto
hitokoto2014 at gmail.com
Thu Jun 12 03:13:16 EDT 2014
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]] )
More information about the Python-list
mailing list