Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Columns in a table
#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


Messages In This Thread
Columns in a table - by Reldaing - Nov-19-2019, 09:13 PM
RE: Columns in a table - by jefsummers - Nov-19-2019, 09:31 PM
RE: Columns in a table - by Gribouillis - Nov-19-2019, 09:47 PM
RE: Columns in a table - by Reldaing - Nov-19-2019, 09:48 PM
RE: Columns in a table - by jefsummers - Nov-19-2019, 09:51 PM
RE: Columns in a table - by Reldaing - Nov-19-2019, 09:57 PM
RE: Columns in a table - by newbieAuggie2019 - Nov-20-2019, 07:39 AM
RE: Columns in a table - by ThomasL - Nov-20-2019, 12:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a table with different sizes of columns in MS word pepe 8 1,636 Dec-08-2023, 07:31 PM
Last Post: Pedroski55
  Sum the values in a pandas pivot table specific columns klllmmm 1 4,669 Nov-19-2021, 04:43 PM
Last Post: klllmmm
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,193 Jul-02-2021, 02:19 PM
Last Post: xtc14
  Printing lists in a table, rows and columns randy_shooflay 6 6,362 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