Re: Slicing Arrays in this way

2007-05-03 Thread Tobiah
John Machin wrote:
> On May 3, 8:55 am, Steven D'Aprano
> <[EMAIL PROTECTED]> 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 "", line 1, in 
>> 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing Arrays in this way

2007-05-03 Thread Michael Hoffman
John Machin wrote:
> On May 3, 10:21 am, Michael Hoffman <[EMAIL PROTECTED]> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing Arrays in this way

2007-05-02 Thread Steven D'Aprano
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 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing Arrays in this way

2007-05-02 Thread James Stroud
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing Arrays in this way

2007-05-02 Thread John Machin
On May 3, 10:21 am, Michael Hoffman <[EMAIL PROTECTED]> 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])
>>>


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing Arrays in this way

2007-05-02 Thread Michael Hoffman
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing Arrays in this way

2007-05-02 Thread Ian Clark
Yeah, having an elegant_solution() function would solve soo many of my
problems. ;)

Ian
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing Arrays in this way

2007-05-02 Thread John Machin
On May 3, 8:55 am, Steven D'Aprano
<[EMAIL PROTECTED]> 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 "", line 1, in 
> NameError: name 'elegant_solution' is not defined
>

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



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing Arrays in this way

2007-05-02 Thread Steven D'Aprano
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 "", line 1, in 
NameError: name 'elegant_solution' is not defined



-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing Arrays in this way

2007-05-02 Thread Tobiah

I'm a retard.  Disregard.

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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing Arrays in this way

2007-05-02 Thread Tobiah
John Machin wrote:
> On May 3, 8:03 am, Tobiah <[EMAIL PROTECTED]> 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing Arrays in this way

2007-05-02 Thread Tobiah
Matimus wrote:
> On May 2, 3:03 pm, Tobiah <[EMAIL PROTECTED]> 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing Arrays in this way

2007-05-02 Thread John Machin
On May 3, 8:03 am, Tobiah <[EMAIL PROTECTED]> 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]


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing Arrays in this way

2007-05-02 Thread Matimus
On May 2, 3:03 pm, Tobiah <[EMAIL PROTECTED]> 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]]

-- 
http://mail.python.org/mailman/listinfo/python-list


Slicing Arrays in this way

2007-05-02 Thread Tobiah

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

-- 
http://mail.python.org/mailman/listinfo/python-list