- 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
importnumpyasnp
classShuffleMatrix:
'''shuffle matrix '''
defshuffleMatrix(self, matrix, idx: int):
rows=len(matrix)
cols=len(matrix[0])
shuffleMatrix=np.zeros((rows, cols), type(matrix))
# matix shuffling
forrinrange(0, rows):
k=0
forcinrange(0, cols):
shuffleMatrix[r][c] =matrix[r][idx[k]]
k=k+1
returnshuffleMatrix
defreshuffleMatrix(self, matrix, idx: int):
rows=len(matrix)
cols=len(matrix[0])
orignalMatrix=np.zeros((rows, cols), type(matrix))
# matix shuffling
forrinrange(0, rows):
k=0
forcinrange(0, cols):
orignalMatrix[r][idx[k]] =matrix[r][c]
k=k+1
returnorignalMatrix
classShuffleArray:
''' shuffle the values in array and reShuffle the values'''
defshuffle(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
foriinrange(0, lenArray):
suffledArr.append(arr[index[i]])
returnsuffledArr
defreshuffle(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))
foriinrange(0, lenArray):
orignalArr[index[i]] =shuffledArr[i]
returnorignalArr
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])