I have a list containing sequences of various lengths. The pattern of a sequence is as follows:
x_k, y_k, ..., x_k_i, y_k_i, ... z_k
For example, a list having 4 sequences with lengths: 3, 3, 5, and 7 is as follows:
input_list = ['x_1', 'y_1', 'z_1',
'x_2', 'y_2', 'z_2',
'x_3_1', 'y_3_1', 'x_3_2', 'y_3_2', 'z_3',
'x_4_1', 'y_4_1', 'x_4_2', 'y_4_2', 'x_4_3', 'y_4_3', 'z_4']
I need to shuffle the list, such that the order of sequences is shuffled, but the entries within a sequence is not shuffled.
For example, a candidate output would be as follows:
shuffled_list = ['x_3_1', 'y_3_1', 'x_3_2', 'y_3_2', 'z_3',
'x_1', 'y_1', 'z_1',
'x_4_1', 'y_4_1', 'x_4_2', 'y_4_2', 'x_4_3', 'y_4_3', 'z_4',
'x_2', 'y_2', 'z_2']
One way to achieve this would be by saving each sequence as a separate list, and then having a nested list represent all the sequences. Then, one by one randomly removing a list (i.e., a sequence) from the nested list and appending the removed list's elements in the final shuffled list.
Is there a more efficient way to achieve the same?