shuffle list with repetition constraint 2-back python -
i have next list:
a=['airplane','track','car','train']
i create list presents every item of list twice, prevents item repetitions next 2 rows. means aeroplane can appear after aeroplane long 2 different items in between b candidate:
b=['airplane','track','car','airplane','train','track','car' etc.]
but c not:
c=['airplane,'track','airplane', etc.]
i thinking of kind of bruteforce operation where: 1. duplicated 2. random.shuffle(a) 3. test repetition (maybe below:
curword[n]==curword[n+1]
re-shuffle if true
, start over. (i don't know command instruct python read truth value. imagine if
statement work, in case of false
don't know how i'd instruct python carry on in case, although getting answers particular questions above own knowledge, can see implementation have considered take long list increases.
any suggestions?
thanks in advance help!
if need list 2 copies of each element, there reason why won't work when original list longer 2 elements?
in [138]: a=['airplane','track','car','train'] in [139]: + out[139]: ['airplane', 'track', 'car', 'train', 'airplane', 'track', 'car', 'train']
if asking more abstract question of, "how sample space of permutations of list elements such don't appear within 2 elements of identical element" next should work.
note getting construction elements appear twice easy a + a
, can worry restricting permutations of a + a
-- no need overthink "how 2 of each" part of problem.
import random def valid_duplicate_spacing(x): i, elem in enumerate(x): if elem in x[i+1:i+3]: homecoming false homecoming true def sample_permutations_with_duplicate_spacing(seq): sample_seq = seq + seq random.shuffle(sample_seq) while not valid_duplicate_spacing(sample_seq): random.shuffle(sample_seq) homecoming sample_seq
then can used follows:
in [165]: sample_permutations_with_duplicate_spacing(a) out[165]: ['airplane', 'train', 'track', 'car', 'train', 'track', 'car', 'airplane'] in [166]: sample_permutations_with_duplicate_spacing(a) out[166]: ['train', 'airplane', 'car', 'track', 'train', 'airplane', 'track', 'car']
if you're talking simply randomly sampling list, such sample not replaced 2 next draws, utilize generator:
import random def draw_with_delayed_replacement(seq): drawn = random.choice(seq) rejectables = [drawn] yield drawn drawn = random.choice(seq) while drawn in rejectables: drawn = random.choice(seq) rejectables.append(drawn) yield drawn while true: drawn = random.choice(seq) if drawn in rejectables: go on else: rejectables.pop(0) rejectables.append(drawn) yield drawn
then can following:
in [146]: foo = draw_with_delayed_replacement(a) in [147]: foo.next() out[147]: 'car' in [148]: foo.next() out[148]: 'train' in [149]: foo.next() out[149]: 'track' in [150]: foo.next() out[150]: 'car' in [151]: foo.next() out[151]: 'train' in [152]: foo.next() out[152]: 'track' in [153]: foo.next() out[153]: 'car' in [154]: foo.next() out[154]: 'airplane' in [155]: foo.next() out[155]: 'track' in [156]: foo.next() out[156]: 'train'
however, in case can't guarantee you're going sample each element appears twice, , may inefficient little lists.
python shuffle
No comments:
Post a Comment