Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Columns in a table
#1
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()
Reply
#2
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()
Reply
#3
Why not
a[0], a[1] = a[1], a[0]
Reply
#4
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
Reply
#5
It is doing what you are telling it to - swapping rows 3 and 6. Remember the arrays are zero based. What are you expecting?
Reply
#6
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...
Reply
#7
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 238 Jul-02-2021, 02:19 PM
Last Post: xtc14
  Fetch Oracle DB rows & print it in HTML file with table's col headers in table format tssr_2001 1 959 Sep-04-2020, 01:39 PM
Last Post: ibreeden
  Mysql CREATE TABLE IF NOT EXISTS dynamic table name nisusavi 0 670 Apr-29-2020, 06:45 PM
Last Post: nisusavi
  MYSQL how to assign a table for each user in my username table database? YoshikageKira 1 1,229 Dec-26-2019, 05:57 AM
Last Post: buran
  Printing lists in a table, rows and columns randy_shooflay 6 3,266 Sep-05-2018, 07:59 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020