Slicing Arrays in this way

Discussion in 'Python' started by Tobiah, May 2, 2007.

  1. Tobiah

    Tobiah Guest


    >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])

    [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]



    --
    Posted via a free Usenet account from http://www.teranews.com
    Tobiah, May 2, 2007
    #1
    1. Advertising

  2. Tobiah

    Matimus Guest

    On May 2, 3:03 pm, Tobiah <> wrote:
    > >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])

    > [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
    >
    > --
    > Posted via a free Usenet account fromhttp://www.teranews.com


    >>> seq = range(1,11)
    >>> seq

    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> zip( seq[0::2],seq[1::2] )

    [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

    if you _really_ need lists then...
    >>> map(list, zip( seq[0::2],seq[1::2] ))

    [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
    Matimus, May 2, 2007
    #2
    1. Advertising

  3. Tobiah

    John Machin Guest

    On May 3, 8:03 am, Tobiah <> wrote:
    > >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])

    > [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
    >


    What is your definition of "elegant"? What about other dimensions of
    code quality like "robust" and "fast"?

    What have you tried?

    Here's one possibility:
    zip(source[::2], source[1::2])
    [I'm presuming you won't be upset by getting tuples instead of lists]
    John Machin, May 2, 2007
    #3
  4. Tobiah

    Tobiah Guest

    Matimus wrote:
    > On May 2, 3:03 pm, Tobiah <> wrote:
    >> >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])

    >> [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
    >>
    >> --
    >> Posted via a free Usenet account fromhttp://www.teranews.com

    >
    >>>> seq = range(1,11)
    >>>> seq

    > [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>>> zip( seq[0::2],seq[1::2] )

    > [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
    >
    > if you _really_ need lists then...
    >>>> map(list, zip( seq[0::2],seq[1::2] ))

    > [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
    >


    I had come up with:

    [[a[x], a[x + 1]] for x in range(0, 10, 2)]

    I was hoping for something a little more concise.
    Something like

    list[::2:2] if that existed.


    --
    Posted via a free Usenet account from http://www.teranews.com
    Tobiah, May 2, 2007
    #4
  5. Tobiah

    Tobiah Guest

    John Machin wrote:
    > On May 3, 8:03 am, Tobiah <> wrote:
    >> >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])

    >> [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
    >>

    >
    > What is your definition of "elegant"? What about other dimensions of
    > code quality like "robust" and "fast"?
    >
    > What have you tried?
    >
    > Here's one possibility:
    > zip(source[::2], source[1::2])
    > [I'm presuming you won't be upset by getting tuples instead of lists]


    I like it, and it fits my definition of elegant.

    --
    Posted via a free Usenet account from http://www.teranews.com
    Tobiah, May 2, 2007
    #5
  6. Tobiah

    Tobiah Guest

    Tobiah, May 2, 2007
    #6
  7. On Wed, 02 May 2007 15:03:24 -0700, Tobiah wrote:

    >
    > >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])

    > [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]


    Wow! That's impressive. What version of Python are you using? When I try
    it, I get this:

    >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'elegant_solution' is not defined



    --
    Steven.
    Steven D'Aprano, May 2, 2007
    #7
  8. Tobiah

    John Machin Guest

    On May 3, 8:55 am, Steven D'Aprano
    <> wrote:
    > On Wed, 02 May 2007 15:03:24 -0700, Tobiah wrote:
    >
    > > >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])

    > > [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]

    >
    > Wow! That's impressive. What version of Python are you using? When I try
    > it, I get this:
    >
    > >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])

    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > NameError: name 'elegant_solution' is not defined
    >


    The OP has already confessed. Don't rub it in.
    John Machin, May 3, 2007
    #8
  9. Tobiah

    Ian Clark Guest

    Yeah, having an elegant_solution() function would solve soo many of my
    problems. ;)

    Ian
    Ian Clark, May 3, 2007
    #9
  10. Tobiah wrote:
    >
    > >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])

    > [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]


    That's not an array, it's a list. See the array module for arrays
    (fixed-length, unlike variable-length lists).
    --
    Michael Hoffman
    Michael Hoffman, May 3, 2007
    #10
  11. Tobiah

    John Machin Guest

    On May 3, 10:21 am, Michael Hoffman <> wrote:
    > Tobiah wrote:
    >
    > > >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])

    > > [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]

    >
    > That's not an array, it's a list. See the array module for arrays
    > (fixed-length, unlike variable-length lists).


    You must have your very own definitions of "fixed-length" and
    "unlike".

    >>> import array
    >>> fixed = array.array('b')
    >>> fixed.append(42)
    >>> fixed.extend([0, 1, 127])
    >>> fixed

    array('b', [42, 0, 1, 127])
    >>> fixed.append(2)
    >>> fixed

    array('b', [42, 0, 1, 127, 2])
    >>> fixed[2:4] = array.array('b', [8])
    >>> fixed

    array('b', [42, 0, 8, 2])
    >>>
    John Machin, May 3, 2007
    #11
  12. Tobiah

    James Stroud Guest

    Tobiah wrote:
    >
    > >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])

    > [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
    >
    >


    Here's one I use:

    def elegant_solution(alist):
    i = iter(alist)
    return [[j, i.next()] for j in i]


    py> elegant_solution(range(14))
    [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13]]

    James
    James Stroud, May 3, 2007
    #12
  13. On Wed, 02 May 2007 16:01:05 -0700, John Machin wrote:


    > The OP has already confessed. Don't rub it in.


    Sorry, I sent my comment before I received his confession.



    --
    Steven D'Aprano
    Steven D'Aprano, May 3, 2007
    #13
  14. John Machin wrote:
    > On May 3, 10:21 am, Michael Hoffman <> wrote:
    >> Tobiah wrote:
    >>
    >>> >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])
    >>> [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]

    >> That's not an array, it's a list. See the array module for arrays
    >> (fixed-length, unlike variable-length lists).

    >
    > You must have your very own definitions of "fixed-length" and
    > "unlike".


    Sorry, too much time spent with numarray arrays which are documented to
    have immutable size.
    --
    Michael Hoffman
    Michael Hoffman, May 3, 2007
    #14
  15. Tobiah

    Tobiah Guest

    John Machin wrote:
    > On May 3, 8:55 am, Steven D'Aprano
    > <> wrote:
    >> On Wed, 02 May 2007 15:03:24 -0700, Tobiah wrote:
    >>
    >>> >>> elegant_solution([1,2,3,4,5,6,7,8,9,10])
    >>> [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]

    >> Wow! That's impressive. What version of Python are you using? When I try
    >> it, I get this:
    >>
    >>>>> elegant_solution([1,2,3,4,5,6,7,8,9,10])

    >> Traceback (most recent call last):
    >> File "<stdin>", line 1, in <module>
    >> NameError: name 'elegant_solution' is not defined
    >>

    >
    > The OP has already confessed. Don't rub it in.
    >


    Well, my first post made perfect sense. My 'confession'
    involved noticing that I had replied to one respondent
    saying that I wanted something more concise, while
    praising the aptness of the same solution to the next
    poster. Lack of oxygen, I think.

    --
    Posted via a free Usenet account from http://www.teranews.com
    Tobiah, May 3, 2007
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish

    slicing and dicing of byte arrays

    Andy Fish, Jun 21, 2004, in forum: Java
    Replies:
    2
    Views:
    360
    Michael Borgwardt
    Jun 22, 2004
  2. Replies:
    75
    Views:
    1,259
    =?utf-8?q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Apr 28, 2005
  3. John Henry
    Replies:
    19
    Views:
    387
    Dennis Lee Bieber
    Dec 1, 2006
  4. Martin Manns

    Slicing wrapped numpy arrays

    Martin Manns, Jan 13, 2008, in forum: Python
    Replies:
    3
    Views:
    351
    Robert Kern
    Jan 14, 2008
  5. Philipp
    Replies:
    21
    Views:
    1,101
    Philipp
    Jan 20, 2009
Loading...

Share This Page