Slicing Arrays in this way

M

Matimus

J

John Machin

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

Tobiah

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

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

Tobiah

John said:
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.
 
J

John Machin

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

Ian Clark

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

Ian
 
J

John Machin

Tobiah said:
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])
 
J

James Stroud

Tobiah said:
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
 
M

Michael Hoffman

John said:
Tobiah said:
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.
 
T

Tobiah

John said:
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.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top