Python Forum
2d Array adds last element to entire list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
2d Array adds last element to entire list
#1
Good Afternoon!

First time poster!

I keep running into this simple problem when trying to fill my 2d array from my SQL. For some reason it will not store the values into my variables. I am really not too sure what else to do here? It seems painfully obvious and is probably staring at me right in the face.


Here is my function -

def GetPlayerData(conn,cur):
    
    rows, cols = (6, 6)
    Players = [[0]*cols]*rows
    
    
    cur.execute('SELECT * FROM Players')
      
    for x in range (1,6):
        nextline = cur.fetchone()
        Players[x][0]= nextline['Players']
        Players[x][1]= nextline['Total_Cash']
        Players[x][2]= nextline['Risk_Level']
        print(Players[x][0])
        
    print(Players[2][2])

    return Players
My first print(Players[x][0]) Displays the entire list as it cycles through x.
My second print(Players[2][2]) shows the last 'players' in the list. every single players[1-5] is stored as the last item in my database.

Thank you
Reply


Messages In This Thread
2d Array adds last element to entire list - by waiteup - Nov-19-2020, 05:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 4 127 2 hours ago
Last Post: sawtooth500
  Make entire script run again every 45 mo NDillard 0 292 Jan-23-2024, 09:40 PM
Last Post: NDillard
  list in dicitonary element problem jacksfrustration 3 625 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Find (each) element from a list in a file tester_V 3 1,155 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,713 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  functional LEDs in an array or list? // RPi user Doczu 5 1,521 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,158 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  How to find the second lowest element in the list? Anonymous 3 1,903 May-31-2022, 01:58 PM
Last Post: Larz60+
  check if element is in a list in a dictionary value ambrozote 4 1,879 May-11-2022, 06:05 PM
Last Post: deanhystad
  Reiszing figure to occupy entire frame fishbackp 0 1,348 Jan-06-2022, 10:33 PM
Last Post: fishbackp

Forum Jump:

User Panel Messages

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