Pseudo-randomly pick an element from a list only if it was not chosen yet

时间:2016-02-12 20:10:36

标签: python python-3.x random choice

Is there a way to "pseudo"-randomly select an element from a list that wasn't chosen before? I know about the choice function, which returns a random item from the list, but without taking into account previous chosen items. I could keep track of which elements were already picked, and keep randomly choose another not yet selected item, but this might include nested loops, etc.

I could also for example remove the element chose from the list at each iteration, but this does not seem a good solution, too.

My question is: is there a "aware" choice function that selects only items that weren't chosen before? Note that I'm not asking how to implement such a function, but possible solutions are of course well-accepted too.

2 个答案:

答案 0 :(得分:1)

As others have said, you can shuffle the list and then use the protected override void OnPaint(PaintEventArgs e) { //base.OnPaint(e); LockUnlockBitsExample(e) } method. Here is an example:

list.pop()

答案 1 :(得分:0)

Shuffle the list and pop elements from the top. That will only produce each list element once.