Random Remove List

Good evening! Still new to programming, so naked with me. A simple question, I hope!

I have data in a list, for example:

L = [(3,4,5),(1,4,5),(1,2,3),(1,2,3)] 

I need to arbitrarily choose size 2, so I wanted to use:

 import random t1 = random.sample(set(L),2) 

Now T1 is a list of random values, but I wanted to remove those that were accidentally extracted from our original list from our original list. I could do a linear loop, but for the task I'm trying to do for a larger list. so the lead time will be forever!

Any suggestions on how to do this?

+4
source share
2 answers
 t1 = [L.pop(random.randrange(len(L))) for _ in xrange(2)] 

does not change the order of the remaining elements in L

+6
source

One option is to view the list and then place the first two items.

 import random L = [(3,4,5),(1,4,5),(1,2,3),(1,2,3)] random.shuffle(L) t1 = L[0:2] 
+3
source

Source: https://habr.com/ru/post/1480059/


All Articles