0

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

Proeliorr
  • 1
  • 1
  • Can you please [edit] your question and put there sample input and expected output? – Andrej Kesely Aug 29 '23 at 21:05
  • `T = Flexlist(new_list)` When this code executes, `new_list` is empty, so trying to access any index in the list will be out of range. – John Gordon Aug 29 '23 at 21:22
  • This looks somewhat like a Caesar chiffre. It would be way easier if you'd just convert the string input into their ASCII codes and substracting a threshold. If you really want to use a list you can do all this stuff in just one list comprehension. `input_characters_list` is redundant since strings are like lists. – lord_haffi Aug 29 '23 at 21:35
  • Would look something like this: `str_input_indices = [base_list.index(char) for char in str_input]`. You could even include the shift here: `str_input_indices = [(base_list.index(char) + shift + len(base_list)) % len(base_list) for char in str_input]`. This should work with positive and negative values of `shift`. – lord_haffi Aug 29 '23 at 21:38
  • Don't use `input` as a variable. It's the name of a built-in function. After you do `input=[]` you won't be able to do something like `response = input("Enter a value")` – Barmar Aug 29 '23 at 21:40
  • FYI, your `input_characters_list()` function is equivalent to `input.extend(str_input)` – Barmar Aug 29 '23 at 21:42
  • @JohnGordon - Is it empty? I used 'shifting' function before defining T variable: T = Flexlist(new_list) so the list should not be empty – Proeliorr Aug 30 '23 at 17:18
  • Inside `shifting()`, the variable `new_list` is local. It does not affect the global variable of the same name. So yes, `new_list` is empty. – John Gordon Aug 30 '23 at 17:20
  • @JohnGordon - and that was the solution. I changed the code in shifting() definition to global and the code worked as it should. Thanks for that tip. – Proeliorr Aug 30 '23 at 18:20
  • 1
    @lord_haffi - It is a good idea. I will take it in consideration. Currently, I am during one of the Python courses, and task like Caesar chiffre is currently taken in consideration... but I do not want to watch the solution even if I am struggling and writing a post on Stack :). So, the code works. Now, I will try the ASCII approach. Thank you for your answer. – Proeliorr Aug 30 '23 at 18:26

0 Answers0