2

This question is two-folded. Firstly, for my understanding, the idea to uniformly remove 10% from a given list is like the following. (1): count the 10% * totul number of elements (2): randomly and uniformly remove element one by one until the number of elements in the list is at most 90% * totul number of elements I.e.,

while (the current number of elements is bigger than 90% * totul number of elements)
            randomly and uniformly generate an element from the current list and remove it.

Is the above correct? Does this more sound like without replacement? Or is there any built-in python function I can use. Secondly, following this link, Very fast sampling from a set with fixed number of elements in python I got the idea that removing elements from a list in python is costly. So, is there any better way to get around this? Many thanks.

Community
  • 1
  • 1
qaz
  • 35
  • 6

2 Answers2

2

It seems like random.sample would be the way to go...

n_elements = int(len(elements) * 0.9)
randomly_selected = random.sample(elements, n_elements)

This gets around the "removing elements from a list in python is costly" problem by creating a new list instead.

mgilson
  • 300,191
  • 65
  • 633
  • 696
2

I did this by getting how big 90% of the list is, random.shuffleing the list, then reassigning the list from the beginning through n (90% of the list).

n = int(len(elements) * 0.9)
random.shuffle(elements)
elements = elements[:n]
michaelpri
  • 3,521
  • 4
  • 30
  • 46