1

Let's say I have this list with size equal to 10 :

List=[0,2,3,7,6,8,9,15,20,30,40]

I want to reduce the size of the list to a fixed one. For this example let's say to 4. Since python does not support float slice indices, is there a workaround to extract elements of this list every 10/4=2.5 element.

I would like a result that looks like this for example:

New_List =[0,6,15,40]

I tried this :

New_List = [n for k,n in enumerate(List) if k+1 in [1, round(len(List)*0.33), round(len(List)*.66), len(List)]]

This only extracts 4 elements. The new size can change and can be bigger than 4 so I want a better workaround to extract elements of this list every x element where x is a float.

Mejdi Dallel
  • 573
  • 9
  • 24
  • Not quiet. For example when I execute this code on this list : a=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,28] idx = np.round(np.linspace(0, len(a)-1, 8)).astype(int) idx I get : array([ 0, 3, 5, 8, 11, 14, 16, 19]) but 19 is not in the original list – Mejdi Dallel Nov 15 '21 at 10:06
  • that result corresponds to the indices. then you still need to index your original list: e.g. `np.array(a)[idx]` – mcsoini Nov 15 '21 at 10:09
  • Oh okay my bad. Thank you this answers my question. – Mejdi Dallel Nov 15 '21 at 10:14

0 Answers0