-1

I recently got help with fixing a function that changes elements in a list when printing said list. However, my program demands me being able to revert whatever changes I've made to an element in the list. Judging from the code, this should be doable with what I have, but it seems I can't alter the list once it has been altered once. The affected code looks like this:

def seatingmap():

# The parameters of seating-plan
rows = 6
seats = 4

seat_rows = [i for i in range(1, rows * seats + 1)]
seat_rows = [seat_rows[i:i + seats] for i in range(0, len(seat_rows), seats)]

return seat_rows


def find_in2Dlist(lst, item):
for index, sublist in enumerate(lst):
    try:
        aname = sublist.index(item)
        return [index, aname]
    except:
        continue
return None


def printList(mlist, chosenseat):
i = 0
temp = mlist.copy()
while i < len(chosenseat):
    findseat = find_in2Dlist(mlist, chosenseat[i])
    if findseat == None:
        i += 1
    else:
        temp[findseat[0]][findseat[1]] = '*' + str(temp[findseat[0]][findseat[1]]) + '*'
    i += 1

for idx, row in enumerate(temp):
    if idx == len(temp) // 2:
        print("{:^{}}".format('↓ TYST AVD ↓', (len(row) * 4) - 2))
    if idx % 2 == 1:
        row = row[::-1]
    print(("{:<4}" * len(row)).format(*row))


list = seatingmap()
number = [2]
printList(list, number)
number = [2,4]
printList(list, number)

Edit

If I for example run the code above, it changes this:

1   2   3   4   
8   7   6   5   
9   10  11  12  
 ↓ TYST AVD ↓ 
16  15  14  13  
17  18  19  20  
24  23  22  21

To this:

1   *2* 3   *4* 
8   7   6   5   
9   10  11  12  
 ↓ TYST AVD ↓ 
16  15  14  13  
17  18  19  20  
24  23  22  21

Which is good. Although, now I want be able to change it back to let's say:

1   2   3   *4* 
8   7   6   5   
9   10  11  12  
 ↓ TYST AVD ↓ 
16  15  14  13  
17  18  19  20  
24  23  22  21

By removing the "2" from "chosenseats". This is where I'm stuck, as when I attempt to do this, it prints out the same list as before the 2 was removed.Is there any way I can go about this issue?

Hvar01
  • 5
  • 2
  • can you provide a [mcve]? – coderoftheday Nov 29 '20 at 20:07
  • See if my edit of the question helps. If you need any more info, let me know! – Hvar01 Nov 29 '20 at 20:48
  • Why are you altering the list at all? If you hold a list of chosen_seats and print the list of available seats you can just print the asterisks at the appropriate time. Also, you shouldn't call your list `list` that's a keyword and will cause you problems later on. – JeffUK Nov 29 '20 at 21:00
  • Your code is not properly formatted python code, the indentations are wrong. – JeffUK Nov 29 '20 at 21:01
  • Does this answer your question? [python: changes to my copy variable affect the original variable](https://stackoverflow.com/questions/19951816/python-changes-to-my-copy-variable-affect-the-original-variable) technically this solves the root cause of your current question, but as per my answer there are other things to think about. – JeffUK Nov 29 '20 at 21:36

1 Answers1

0

It could be fixed by simply refreshing the list each time you call printList or copying the list contents not the list, I feel that is an incomplete answer.

As far as I can see the below achieves the same result with a lot less code. It could be shortened more using list comprehensions etc. but I'm focussing here on removing unnecessary functions rather than just reducing the number of lines of code.

def generate_seatmap(rows = 6, seats_per_row = 4):

    seatmap = []
    for row in range(0,rows):
        start_of_row = row*seats_per_row+1
        seatmap.append([seat for seat in range(start_of_row,start_of_row+seats_per_row)])
        
    return seatmap

def print_seatmap(seatmap,chosenseats=[]):

    for row in seatmap:
        if seatmap.index(row)%2: #reverse odd numbered rows
            row.reverse()
            
        for seat in row:
            #add asterisks for seats we have chosen
            printable_seat = '*'+str(seat)+'*' if seat in chosenseats else str(seat)

            #add fewer spaces the longer our seat label now is up to a max of 4
            print(printable_seat, end = ' '*(4-len(printable_seat)))
            
        print("")

print_seatmap(generate_seatmap(),[1,2,24,23])
JeffUK
  • 4,107
  • 2
  • 20
  • 34