About python while statement and pop()

H

hito koto

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
 
D

Dave Angel

hito koto said:
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.
 
V

Vincent Vande Vyvre

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)
 
C

Chris Angelico

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
 
H

hito koto

2014年6月12日木曜日 12時58分27秒 UTC+9 Chris Angelico:
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:.... 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]])
 
C

Chris Angelico

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
 
S

Steven D'Aprano

I want to use while statement,

for example:... 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 += 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.
 
H

hito koto

2014年6月12日木曜日 14時43分42秒 UTC+9 Steven D'Aprano:
I want to use while statement,

for example:
... y = []
... while x !=[]:
... y.append(x.pop())
... return y
[[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]]
[] 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 += 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.


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]] )
 
H

hito koto

2014年6月12日木曜日 14時43分42秒 UTC+9 Steven D'Aprano:
I want to use while statement,

for example:
... y = []
... while x !=[]:
... y.append(x.pop())
... return y
[[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]]
[] 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 += 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.


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]] )
 
H

hito koto

2014年6月12日木曜日 14時43分42秒 UTC+9 Steven D'Aprano:
I want to use while statement,

for example:
... y = []
... while x !=[]:
... y.append(x.pop())
... return y
[[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]]
[] 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 += 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.


Hi,
Thank you!
 
M

Mark H Harris

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):
[1, 2, 3, 4, 5, 6, 7]
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)
[]
 
C

Chris Angelico

Consider this generator variation:
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
 
M

Mark H Harris

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:
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 ===================
 
W

wxjmfauth

Le jeudi 12 juin 2014 19:16:19 UTC+2, Mark H. Harris a écrit :
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:



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])





[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top