Nico Autia - 1 year ago 52

Python Question

I have a range of possible values, for example:

`possible_values = range(100)`

I have a list with unsystematic (but unique) numbers within that range, for example:

`somelist = [0, 5, 10, 15, 20, 33, 77, 99]`

I want to create a new list of length < len(somelist) including a subset of these values but as equally distributed as possible over the range of possible values. For example:

`length_newlist = 2`

newlist = some_function(somelist, length_newlist, possible_values)

print(newlist)

Which would then ideally output something like

`[33, 77]`

So I neither want a random sample nor a sample that chosen from equally spaced integers. I'd like to have a sample based on a distribution (here an uniform distribution) in regard to an interval of possible values.

Is there a function or an easy way to achieve this?

Answer Source

What about the closest values of your subset to certain list's pivots? ie:

```
def some_function(somelist, length_list, possible_values):
a = min(possible_values)
b = max(possible_values)
chunk_size = (b-a)/(length_list+1)
new_list = []
for i in range(1,length_list+1):
index = a+i*chunk_size
new_list.append(min(somelist, key=lambda x:abs(x-index)))
return new_list
possible_values = range(100)
somelist = [0, 5, 10, 15, 20, 33, 77, 99]
length_newlist = 2
newlist = some_function(somelist, length_newlist, possible_values)
print(newlist)
```

In any case, I'd also recommend to take a look to numpy's random sampling functions, that could help you as well.