-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_shuffle_reshuffle.py
70 lines (54 loc) · 2.12 KB
/
array_shuffle_reshuffle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import numpy as np
class ShuffleMatrix:
'''shuffle matrix '''
def shuffleMatrix(self, matrix, idx: int):
rows = len(matrix)
cols = len(matrix[0])
shuffleMatrix = np.zeros((rows, cols), type(matrix))
# matix shuffling
for r in range(0, rows):
k = 0
for c in range(0, cols):
shuffleMatrix[r][c] = matrix[r][idx[k]]
k = k + 1
return shuffleMatrix
def reshuffleMatrix(self, matrix, idx: int):
rows = len(matrix)
cols = len(matrix[0])
orignalMatrix = np.zeros((rows, cols), type(matrix))
# matix shuffling
for r in range(0, rows):
k = 0
for c in range(0, cols):
orignalMatrix[r][idx[k]] = matrix[r][c]
k = k + 1
return orignalMatrix
class ShuffleArray:
''' shuffle the values in array and reShuffle the values'''
def shuffle(self, arr, index: int):
'''shuffle the array args {arr:[list or numpy.array], index:[list or numpy.array]}
index will be the how to shuffle the values according to index values
'''
lenArray = len(arr)
suffledArr = [] # empty list to store the shuffled list
for i in range(0, lenArray):
suffledArr.append(arr[index[i]])
return suffledArr
def reshuffle(self, shuffledArr, index: int):
'''Reshuffle the array to it's orignal state args = {arr:[list or numpy.array], index:[list or numpy.array]}
index will be the how to reshuffle the values according through index values'''
lenArray = len(shuffledArr)
orignalArr = np.zeros(lenArray, dtype=type(shuffledArr))
for i in range(0, lenArray):
orignalArr[index[i]] = shuffledArr[i]
return orignalArr
lst = [10, 20, 30, 40, 50]
idx = [3, 2, 0, 1, 4]
arrange = ShuffleArray()
lst = arrange.shuffle(lst, idx)
lst = arrange.reshuffle(lst, idx)
matrix = [[2, 5],
[8, 3]]
arrangeMatrix = ShuffleMatrix()
arrangeMatrix.shuffleMatrix(matrix=matrix, idx=[1, 0])
arrangeMatrix.reshuffleMatrix(matrix=matrix, idx=[1, 0])