inkstitch/lib/utils/list.py

16 wiersze
409 B
Python
Czysty Zwykły widok Historia

2023-01-16 19:27:06 +00:00
from random import randrange
def poprandom(sequence):
index = randrange(len(sequence))
item = sequence[index]
# It's O(1) to pop the last item, and O(n) to pop any other item. So we'll
# always pop the last item and put it in the slot vacated by the item we're
# popping.
last_item = sequence.pop()
if index < len(sequence):
sequence[index] = last_item
return item