Error during indexing a list with another list

58 Views Asked by At

I am trying to encode a text using by using indices stored as a list and refer that indices as a value of another list.

There is a code:

#The part of a code that creates indices for input text referring to a base list and stores them into a new list. 


def shifting(list, shift_value):
    new_list = list[shift_value:] + list[:shift_value]
    print(new_list)

def input_characters_list(str_input, input):
    for index in str_input:
        input += index

def create_base_list_indices(str_input, base_list, input, base_list_indices):
    for idx, value in enumerate(str_input):
        new_idx = base_list.index(input[idx])
        base_list_indices.append(new_idx)
        #creates a list that contains indices of unshifted characters to be able to encode them


"""
"""

str_input = 'dfgl'
base_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
new_list = [] #contains a list of a shifted letters
input = []    #contains a list of input characters from input str
base_list_indices = []     #contains a list of indices of input characters from a base list
encoded_str =[]

# shifting(base_list, shift_value=2)

input_characters_list(str_input, input) # - creates input variable

create_base_list_indices(str_input, base_list, input, base_list_indices)


I was trying to implement this but several error occure:

  1. When i am trying to use class from the link above
class Flexlist(list):
    def __getitem__(self, keys):
        if isinstance(keys, (int, slice)): return list.__getitem__(self, keys)
        return [self[k] for k in keys]

T = Flexlist(new_list)
L = T[base_list_indices]
print(L)

I receive "IndexError: list index out of range"

  1. The same error occurs when I am trying to create list:
# T = [new_list[i] for i in base_list_indices]

I want to obtain 'encoded_str' list by using elements from 'new_list' list using indices from 'input' list

Any clues what do I miss? Cheers

0

There are 0 best solutions below