4

Say you have a list [1,2,3,...,99,100]

import numpy as np
ls = np.linspace(1,100,100)

Say you fiddle around with the slice notation to find ones that works for you.

print(ls[:2])
>> [ 1.  2.]

print(ls[::2])
>> [  1.   3.   5.   7.  ...  97.  99.]

How can I slice the data in such a way that I can get

[1,2],[3,4],[5,6],[7,8],...,[99,100]

## perhaps not important but my purpose is to adapt this to my dataset, where the values in the lists denote the numerical subscript of another list of values (other_values[index]) that are to be compared. Is it possible to combine slicing with arrays?

2 Answers2

8

You can use the .resize(..,2) option for that. The result is that ls is now a numpy array with 50×2 dimensions:

>>> ls.resize(ls.shape[0]/2,2)
>>> ls
array([[   1.,    2.],
       [   3.,    4.],
       [   5.,    6.],
       [   7.,    8.],
       [   9.,   10.],
       [  11.,   12.],
       [  13.,   14.],
       [  15.,   16.],
       [  17.,   18.],
       [  19.,   20.],
       [  21.,   22.],
       [  23.,   24.],
       [  25.,   26.],
       [  27.,   28.],
       [  29.,   30.],
       [  31.,   32.],
       [  33.,   34.],
       [  35.,   36.],
       [  37.,   38.],
       [  39.,   40.],
       [  41.,   42.],
       [  43.,   44.],
       [  45.,   46.],
       [  47.,   48.],
       [  49.,   50.],
       [  51.,   52.],
       [  53.,   54.],
       [  55.,   56.],
       [  57.,   58.],
       [  59.,   60.],
       [  61.,   62.],
       [  63.,   64.],
       [  65.,   66.],
       [  67.,   68.],
       [  69.,   70.],
       [  71.,   72.],
       [  73.,   74.],
       [  75.,   76.],
       [  77.,   78.],
       [  79.,   80.],
       [  81.,   82.],
       [  83.,   84.],
       [  85.,   86.],
       [  87.,   88.],
       [  89.,   90.],
       [  91.,   92.],
       [  93.,   94.],
       [  95.,   96.],
       [  97.,   98.],
       [  99.,  100.]])
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • This is perfect. I can repeat the pattern to iteratively change my window of subscripts `ls.resize(ls.shape[0]/2,4) ls.resize(ls.shape[0]/2,8)` –  Mar 29 '17 at 12:41
  • @mikey: yes. Although the last call will fail since 100 is not dividable by 8. – Willem Van Onsem Mar 29 '17 at 12:47
  • I am trying to make this operation the return statement for a function, the inputs of which are a list `ls` and the number of desired columns `num_col` (`num_col = 2` from your example). I can get the result to work outside of the function, but not with the function. Is this a common issue? –  Mar 29 '17 at 21:40
  • EDIT: If my resized list does not display commas but still splits the entries with a delimiter (output of your code displays differently for me), could that be the issue? –  Mar 29 '17 at 21:49
  • @mikey: no, this should definitely work within a function. Can you edit your question with the relevant code? – Willem Van Onsem Mar 29 '17 at 22:02
  • Since this post is irrelevant to my follow-up, I posted a new question regarding the use of the routine in a function [here](https://stackoverflow.com/questions/43107666/why-does-this-array-reshaping-routine-work-outside-of-a-function-but-not-inside) –  Mar 30 '17 at 02:54
2

something like

list(zip(l[::2], l[1::2]))

returns a list of tuples with your desired pairing

Quickbeam2k1
  • 5,287
  • 2
  • 26
  • 42