About python while statement and pop()

Discussion in 'Python' started by hito koto, Jun 12, 2014.

  1. hito koto

    hito koto Guest

    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
     
    hito koto, Jun 12, 2014
    #1
    1. Advertisements

  2. hito koto

    Dave Angel Guest

    No idea what the question means. Are you just trying to rewrite
    the loop in a python implementation where pop is broken?


    Perhaps you're looking for the extend method.
     
    Dave Angel, Jun 12, 2014
    #2
    1. Advertisements

  3. Le 12/06/2014 05:12, hito koto a écrit :
    Something like that :

    def foo(x):
    return reversed(x)
     
    Vincent Vande Vyvre, Jun 12, 2014
    #3
  4. 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
     
    Chris Angelico, Jun 12, 2014
    #4
  5. hito koto

    hito koto Guest

    2014年6月12日木曜日 12時58分27秒 UTC+9 Chris Angelico:

    I want to use while statement,

    for example:.... y = []
    .... while x !=[]:
    .... y.append(x.pop())
    .... return y
    ....
     
    hito koto, Jun 12, 2014
    #5
  6. 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
     
    Chris Angelico, Jun 12, 2014
    #6

  7. 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.
     
    Steven D'Aprano, Jun 12, 2014
    #7
  8. hito koto

    hito koto Guest

    2014年6月12日木曜日 14時43分42秒 UTC+9 Steven D'Aprano:


    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]] )
     
    hito koto, Jun 12, 2014
    #8
  9. hito koto

    hito koto Guest

    2014年6月12日木曜日 14時43分42秒 UTC+9 Steven D'Aprano:


    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]] )
     
    hito koto, Jun 12, 2014
    #9
  10. hito koto

    hito koto Guest

    2014年6月12日木曜日 14時43分42秒 UTC+9 Steven D'Aprano:


    Hi,
    Thank you!
     
    hito koto, Jun 12, 2014
    #10
  11. 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]

    []
     
    Mark H Harris, Jun 12, 2014
    #11
  12. Consider this generator variation:
    done = False
    while done==False:
    yield L[::-1][:1:]
    L = L[::-1][1::][::-1]
    if len(L)==0: done=True

     
    Mark H Harris, Jun 12, 2014
    #12
  13. Correction:

    while not done:

    Better Python and not bad English, either.


    Marko
     
    Marko Rauhamaa, Jun 12, 2014
    #13
  14. Why not just "while L"? Or are you deliberately trying to ensure that
    cheating will be detectable?

    ChrisA
     
    Chris Angelico, Jun 12, 2014
    #14
  15. .... and taking Marko's good advice, what I think you really wanted:

    done = False
    while not done:
    yield L[::-1][:1:]
    L = L[::-1][1::][::-1]
    if len(L)==0: done=True

     
    Mark H Harris, Jun 12, 2014
    #15
  16. ;-)
     
    Mark H Harris, Jun 12, 2014
    #16
  17. OK, here it is with Chris' excellent advice:
    while L:
    yield L[::-1][:1:]
    L = L[::-1][1::][::-1]


    ========== aaaaah ===================
     
    Mark H Harris, Jun 12, 2014
    #17
  18. hito koto

    Guest

    Le jeudi 12 juin 2014 19:16:19 UTC+2, Mark H. Harris a écrit :
    [7, 6, 5, 4, 3, 2, 1]

    jmf
     
    , Jun 12, 2014
    #18
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.