Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Slicing Arrays in this way

Reply
Thread Tools

Slicing Arrays in this way

 
 
Tobiah
Guest
Posts: n/a
 
      05-02-2007

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

 
Reply With Quote
 
 
 
 
Matimus
Guest
Posts: n/a
 
      05-02-2007
On May 2, 3:03 pm, Tobiah <(E-Mail Removed)> 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, , (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]]

 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      05-02-2007
On May 3, 8:03 am, Tobiah <(E-Mail Removed)> 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]


 
Reply With Quote
 
Tobiah
Guest
Posts: n/a
 
      05-02-2007
Matimus wrote:
> On May 2, 3:03 pm, Tobiah <(E-Mail Removed)> 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, , (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

 
Reply With Quote
 
Tobiah
Guest
Posts: n/a
 
      05-02-2007
John Machin wrote:
> On May 3, 8:03 am, Tobiah <(E-Mail Removed)> 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

 
Reply With Quote
 
Tobiah
Guest
Posts: n/a
 
      05-02-2007

I'm a retard. Disregard.

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      05-02-2007
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.

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      05-02-2007
On May 3, 8:55 am, Steven D'Aprano
<(E-Mail Removed)> 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.



 
Reply With Quote
 
Ian Clark
Guest
Posts: n/a
 
      05-02-2007
Yeah, having an elegant_solution() function would solve soo many of my
problems.

Ian
 
Reply With Quote
 
Michael Hoffman
Guest
Posts: n/a
 
      05-03-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Slicing wrapped numpy arrays Martin Manns Python 3 01-14-2008 03:15 AM
Is there an easier way to express this list slicing? John Henry Python 19 12-01-2006 08:30 AM
Why Python does *SLICING* the way it does?? seberino@spawar.navy.mil Python 75 04-28-2005 03:31 PM
slicing and dicing of byte arrays Andy Fish Java 2 06-22-2004 08:07 AM



Advertisments