About python while statement and pop()
wxjmfauth at gmail.com
wxjmfauth at gmail.com
Thu Jun 12 14:37:07 EDT 2014
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
More information about the Python-list
mailing list