Python Forum
Columns in a table - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Columns in a table (/thread-22610.html)



Columns in a table - Reldaing - Nov-19-2019

Hi, I'm looking for switching two columns in a table. I made a function , but I doon't know the syntax of an array switching in python . Could you guys help me please? Thx
def changeColumns():
    a = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 1, 2, 3], [7, 8, 9, 1, 2, 3, 4, 5, 6], [2, 3, 1, 5, 6, 4, 8, 9, 7], [5, 6, 4, 8, 9, 7, 2, 3, 1],[8, 9, 7, 2, 3, 1, 5, 6, 4], [3, 1, 2, 6, 4, 5, 9, 7, 8], [6, 4, 5, 9, 7, 8, 3, 1, 2], [9, 7, 8, 3, 1, 2, 6, 4, 5]]
    a[1,2,3,4,5,6,7,8,9][0],a[1,2,3,4,5,6,7,8,9][1]= a[1,2,3,4,5,6,7,8,9][1],a[1,2,3,4,5,6,7,8,9][0] #This is what i want to fix
    for i in range(len(a)):
        for j in range(len(a[i])):
            print(a[i][j], end=' ')
        print()



RE: Columns in a table - jefsummers - Nov-19-2019

a is a list of lists. To swap the first and second lists:
def changeColumns():
    a = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 1, 2, 3], [7, 8, 9, 1, 2, 3, 4, 5, 6], [2, 3, 1, 5, 6, 4, 8, 9, 7], [5, 6, 4, 8, 9, 7, 2, 3, 1],[8, 9, 7, 2, 3, 1, 5, 6, 4], [3, 1, 2, 6, 4, 5, 9, 7, 8], [6, 4, 5, 9, 7, 8, 3, 1, 2], [9, 7, 8, 3, 1, 2, 6, 4, 5]]
    temp = a[1]
    a[1] = a[0]
    a[0] = temp
    print(a)
changeColumns()



RE: Columns in a table - Gribouillis - Nov-19-2019

Why not
a[0], a[1] = a[1], a[0]



RE: Columns in a table - Reldaing - Nov-19-2019

I did manage changing both lines in my table, but the issue is the colums , look:
def changeLines(c,d):
    a = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 1, 2, 3], [7, 8, 9, 1, 2, 3, 4, 5, 6], [2, 3, 1, 5, 6, 4, 8, 9, 7], [5, 6, 4, 8, 9, 7, 2, 3, 1],[8, 9, 7, 2, 3, 1, 5, 6, 4], [3, 1, 2, 6, 4, 5, 9, 7, 8], [6, 4, 5, 9, 7, 8, 3, 1, 2], [9, 7, 8, 3, 1, 2, 6, 4, 5]]
    a[c],a[d]= a[d],a[c]
    for i in range(len(a)):
        for j in range(len(a[i])):
            print(a[i][j], end=' ')
        print()
Output:
>>> changeLines(3,6) 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 3 1 2 6 4 5 9 7 8 5 6 4 8 9 7 2 3 1 8 9 7 2 3 1 5 6 4 2 3 1 5 6 4 8 9 7 6 4 5 9 7 8 3 1 2 9 7 8 3 1 2 6 4 5



RE: Columns in a table - jefsummers - Nov-19-2019

It is doing what you are telling it to - swapping rows 3 and 6. Remember the arrays are zero based. What are you expecting?


RE: Columns in a table - Reldaing - Nov-19-2019

This was just an example of what i've made to switch lines. Now , I want to switch colums but I don't know how to do it, even if i tried...


RE: Columns in a table - newbieAuggie2019 - Nov-20-2019

(Nov-19-2019, 09:57 PM)Reldaing Wrote: This was just an example of what i've made to switch lines. Now , I want to switch colums but I don't know how to do it, even if i tried...
Hi!

Maybe you could do something like this (but probably there is a better way):
def changeColumns(c,d):
    a = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 1, 2, 3], [7, 8, 9, 1, 2, 3, 4, 5, 6], [2, 3, 1, 5, 6, 4, 8, 9, 7], [5, 6, 4, 8, 9, 7, 2, 3, 1],[8, 9, 7, 2, 3, 1, 5, 6, 4], [3, 1, 2, 6, 4, 5, 9, 7, 8], [6, 4, 5, 9, 7, 8, 3, 1, 2], [9, 7, 8, 3, 1, 2, 6, 4, 5]]
    print(f"This is array 'a' with original columns:\n")
    for i in range(len(a)):
        for j in range(len(a[i])):
            print(a[i][j], end=' ')
        print()
    print(f"\nThis is array 'a' with transposed columns:\n")    
    for x in range(0,9):
        a[x][c],a[x][d]= a[x][d],a[x][c]
    for i in range(len(a)):
        for j in range(len(a[i])):
            print(a[i][j], end=' ')
        print()

changeColumns(3,6)
with the self-explanatory output:
Output:
This is array 'a' with original columns: 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 2 3 1 5 6 4 8 9 7 5 6 4 8 9 7 2 3 1 8 9 7 2 3 1 5 6 4 3 1 2 6 4 5 9 7 8 6 4 5 9 7 8 3 1 2 9 7 8 3 1 2 6 4 5 This is array 'a' with transposed columns: 1 2 3 7 5 6 4 8 9 4 5 6 1 8 9 7 2 3 7 8 9 4 2 3 1 5 6 2 3 1 8 6 4 5 9 7 5 6 4 2 9 7 8 3 1 8 9 7 5 3 1 2 6 4 3 1 2 9 4 5 6 7 8 6 4 5 3 7 8 9 1 2 9 7 8 6 1 2 3 4 5 >>>
All the best,


RE: Columns in a table - ThomasL - Nov-20-2019

I would suggest using numpy for this
import numpy as np

a = [[1, 2, 3, 4, 5, 6, 7, 8, 9], 
     [4, 5, 6, 7, 8, 9, 1, 2, 3], 
     [7, 8, 9, 1, 2, 3, 4, 5, 6], 
     [2, 3, 1, 5, 6, 4, 8, 9, 7], 
     [5, 6, 4, 8, 9, 7, 2, 3, 1],
     [8, 9, 7, 2, 3, 1, 5, 6, 4], 
     [3, 1, 2, 6, 4, 5, 9, 7, 8], 
     [6, 4, 5, 9, 7, 8, 3, 1, 2], 
     [9, 7, 8, 3, 1, 2, 6, 4, 5]]

sudoku = np.array(a)

def show(arr):
    for row in arr:
        for num in row:
            print(num, end=" ")
        print()

    
def swap_columns(arr, c1, c2):
    new = arr.copy()
    new[:,[c1, c2]] = new[:,[c2, c1]]
    return new


def swap_rows(arr, r1, r2):
    new = arr.copy()
    new[[r1, r2],:] = new[[r2, r1],:]
    return new
show(sudoku)
Output:
1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 2 3 1 5 6 4 8 9 7 5 6 4 8 9 7 2 3 1 8 9 7 2 3 1 5 6 4 3 1 2 6 4 5 9 7 8 6 4 5 9 7 8 3 1 2 9 7 8 3 1 2 6 4 5
sudoku = swap_columns(sudoku, 0, 8)
show(sudoku)
Output:
9 2 3 4 5 6 7 8 1 3 5 6 7 8 9 1 2 4 6 8 9 1 2 3 4 5 7 7 3 1 5 6 4 8 9 2 1 6 4 8 9 7 2 3 5 4 9 7 2 3 1 5 6 8 8 1 2 6 4 5 9 7 3 2 4 5 9 7 8 3 1 6 5 7 8 3 1 2 6 4 9
sudoku = swap_rows(sudoku, 0, 8)
show(sudoku)
Output:
5 7 8 3 1 2 6 4 9 3 5 6 7 8 9 1 2 4 6 8 9 1 2 3 4 5 7 7 3 1 5 6 4 8 9 2 1 6 4 8 9 7 2 3 5 4 9 7 2 3 1 5 6 8 8 1 2 6 4 5 9 7 3 2 4 5 9 7 8 3 1 6 9 2 3 4 5 6 7 8 1
Of course, sudoku object type is now
Output:
<class 'numpy.ndarray'>
But you can convert it back to python list in case needed.
sudoku = sudoku.tolist()
print(type(sudoku))
Output:
<class 'list'>
But i would build my whole sudoku game on numpy array. imho.