443,965 Members | 1,631 Online
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

# Slicing Arrays in this way

 P: n/a >>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 May 2 '07 #1
14 Replies

 P: n/a On May 2, 3:03 pm, 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 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]] May 2 '07 #2

 P: n/a On May 3, 8:03 am, Tobiah >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] May 2 '07 #3

 P: n/a Matimus wrote: On May 2, 3:03 pm, 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 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 May 2 '07 #4

 P: n/a John Machin wrote: On May 3, 8:03 am, Tobiah >>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 May 2 '07 #5

 P: n/a I'm a retard. Disregard. -- Posted via a free Usenet account from http://www.teranews.com May 2 '07 #6

 P: n/a 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. May 2 '07 #7

 P: n/a On May 3, 8:55 am, Steven D'Aprano >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. May 2 '07 #8

 P: n/a Yeah, having an elegant_solution() function would solve soo many of my problems. ;) Ian May 2 '07 #9

 P: n/a 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 May 3 '07 #10

 P: n/a On May 3, 10:21 am, Michael Hoffman >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 arrayfixed = 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]) >>> May 3 '07 #11

 P: n/a 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] pyelegant_solution(range(14)) [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13]] James May 3 '07 #12

 P: n/a 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 May 3 '07 #13

 P: n/a John Machin wrote: On May 3, 10:21 am, 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). 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 May 3 '07 #14

 P: n/a John Machin wrote: On May 3, 8:55 am, 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 tryit, 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 May 3 '07 #15

### This discussion thread is closed

Replies have been disabled for this discussion.