I have a pairs of names in a list that I want to shuffle for xgboost.
Input: [['A1', 'B1'], ['A2', 'B2'], ['A3', 'B3'], ['A4', 'B4'], ['A5', 'B5'], ['A6', 'B6'], ['A7', 'B7'], ['A8', 'B8']]
Expecting: [['A1', 'B1'], ['B2', 'A2'], ['A3', 'B3'], ['A4', 'B4'], ['B5', 'A5'], ['A6', 'B6'], ['A7', 'B7'], ['B8', 'A8']]
I have tried this and it does produce the results I want but I believe there's an easy way.:
rand_number = random.randint(0, 1000)
tlist_rand = []
for t in tlist:
if rand_number % 3 == 0:
tlist_rand.append(random.sample(t, len(t)))
rand_number = random.randint(0, 1000)
else:
tlist_rand.append(t)
rand_number = random.randint(0, 1000)
Input: [['A1', 'B1'], ['A2', 'B2'], ['A3', 'B3'], ['A4', 'B4'], ['A5', 'B5'], ['A6', 'B6'], ['A7', 'B7'], ['A8', 'B8']]
The rand tuple is: [['A1', 'B1'], ['A2', 'B2'], ['A3', 'B3'], ['B4', 'A4'], ['A5', 'B5'], ['A6', 'B6'], ['A7', 'B7'], ['A8', 'B8']]
You can use random.shuffle
, this function can shuffle list inplace.