Skip to content

Instantly share code, notes, and snippets.

@saintsGrad15
Created January 12, 2022 17:46
Show Gist options
  • Save saintsGrad15/c0a195f62314eb086030714935c77c2c to your computer and use it in GitHub Desktop.
Save saintsGrad15/c0a195f62314eb086030714935c77c2c to your computer and use it in GitHub Desktop.
Return the contents of 'list_' sampled evenly accross its current order.
def sample_list_evenly(list_: List[Any], samples: int) -> List[Any]:
"""
Return the contents of 'list_' sampled evenly accross its current order.
NOTE: Doesn't work perfectly yet...
:param list_: A list of anything.
:param samples: The number of samples to return.
:return: The contents of 'list_' sampled evenly accross its current order.
"""
# How large will each "step" between indices be?
index_step_rate = len(list_) / samples
# Determine the "Evenness Factor", which attempts to keep
# an even distance between samples and either end of the resultant list
# It doesn't seem to work perfectly
if index_step_rate > 2:
evenness_factor = math.floor(index_step_rate / 2)
else:
evenness_factor = 0
raw_indices = [round((index_step_rate * x) + evenness_factor) for x in range(samples)]
indices = sorted(set(raw_indices))
return [list_[index] for index in indices]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment