Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to read a text file into a 2d list and swap the inner list elements

ID: 3919650 • Letter: I

Question

I am trying to read a text file into a 2d list and swap the inner list elements so that the elements in the rows are reversed. I cannot use .reverse or any function like it. I have to iterate through the loops and make changes that way. There seems to be something wrong with my nested while loops because I am trying to store orig_list into temp_list reversed from left to right. I will use three seperate lists while swapping but I need to know what is going wrong with my loop values. Here is my code so far:

def flip(file_in):
count = 0
i = 0
j = 0
row = 0
column = 0
orig_list = []

f = open(file_in, 'r')
for line in f:
lst = []
for char in line:
lst.append(char)
orig_list.append(lst)  

swap_ud_list = [''] * len(orig_list)

temp_list = [[''] * len(orig_list) for i in range(len(orig_list))] #temp_list has correct array size and will act as a way to swap values from orig_list and swap_list
swap_lr_list = [[''] * len(orig_list) for i in range(len(orig_list))]

swap_lr_list = [x[:] for x in orig_list] #swap_lr_list is set to the same thing as orig_list so that their inner list values can be swapped
  
i = 0
j = 0
while i < (len(orig_list)):
while j < (len(orig_list)):
temp_list[i][j] = orig_list[len(orig_list) - 1 - j][j] #maybe switch the j and the i; only one is updating for temp
j += 1
i += 1

When printing orig_list and temp_list values for first element:

[[' ', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', ' ', ' ', ' ', ' ', ' '],

[[' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', '#', ' ', '#', ' ', ' ', ' ', ' '],

Explanation / Answer

def flip(file_in): count = 0 i = 0 j = 0 row = 0 column = 0 orig_list = [] f = open(file_in, 'r') for line in f: lst = [] for char in line: lst.append(char) orig_list.append(lst) swap_ud_list = [''] * len(orig_list) temp_list = [[''] * len(orig_list) for i in range(len(orig_list))] # temp_list has correct array size and will act as a way to swap values from orig_list and swap_list swap_lr_list = [[''] * len(orig_list) for i in range(len(orig_list))] swap_lr_list = [x[:] for x in orig_list] # swap_lr_list is set to the same thing as orig_list so that their inner list values can be swapped i = 0 j = 0 while i < (len(orig_list)): while j < (len(orig_list[i])): temp_list[i][j] = orig_list[i][len(orig_list[i])-j-1] # maybe switch the j and the i; only one is updating for temp j += 1 i += 1
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote