python puts two sets of data together in a fixed order. An instance of shuffle

  • 2021-07-18 08:27:42
  • OfStack

Sometimes it is necessary to put two sets of data, such as features and labels, into one random scramble, but you want to record the order of this scramble, so what should you do? Here is a good way:


b = [1, 2,3, 4, 5,6 , 7,8 ,9]
a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h','i']
c = list(zip(a, b))
print(c)
random.Random(100).shuffle(c)
print(c)
a, b = zip(*c)
print(a)
print(b)

Output:


[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7), ('h', 8), ('i', 9)]
[('a', 1), ('g', 7), ('c', 3), ('i', 9), ('h', 8), ('e', 5), ('f', 6), ('d', 4), ('b', 2)]
('a', 'g', 'c', 'i', 'h', 'e', 'f', 'd', 'b')
(1, 7, 3, 9, 8, 5, 6, 4, 2)

If you re-run this code again, the order of scrambling is still this, and the output remains unchanged.

Here completed the data combination, shuffle, split, is a very effective data processing method.


Related articles: