rearrange elements in a python list based on given array of indices, but not of the same size of the array - TagMerge
1rearrange elements in a python list based on given array of indices, but not of the same size of the arrayrearrange elements in a python list based on given array of indices, but not of the same size of the array

rearrange elements in a python list based on given array of indices, but not of the same size of the array

Asked 5 months ago
0
1 answers

This should do the trick

cipher = ['t', 'h', 'i', 's', 'w', 'o', 'r', 'k', 'i', 'n', 'g']
index = [1, 2, 0]

splitCipher = [cipher[i:i+len(index)] for i in range(0,len(cipher),len(index))]
newCipher = []
for group in splitCipher:
    for i in range(len(index)):
        try:
            newCipher.append(group[index[i]])
        except:
            pass

print(newCipher)

Source: link

Recent Questions on python

    Programming Languages