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
#2
After looking at the screen for 4 hours, I came to the realization that I was making 5 different arrays and I could just use

Player = {}
Player[1,0] instead of Player [1][1]
Thanks guys!
Reply
#3
Your initial problem was that you were not making different lists, but just several copies of one list. The construction: [[]] * num takes the inside list and makes num references to it, but they're all the same list. So each time through was overwriting the previous trips through the loop.

>>> players = [[0] * 4] * 3
>>> players
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> players[0][1] = "x"
>>> players
[[0, 'x', 0, 0], [0, 'x', 0, 0], [0, 'x', 0, 0]]
For this to work, you'd need to make different lists.

>>> players = [[0] * 4 for x in range(3)]
>>> players
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> players[0][1] = "x"
>>> players
[[0, 'x', 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert each element of a list to a string for processing tester_V 6 560 Jun-16-2021, 02:11 AM
Last Post: tester_V
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 657 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  python tool to collect the time measurement of entire code maiya 3 542 Feb-12-2021, 05:39 PM
Last Post: BashBedlam
  How to apply a class method to an entire dataframe column tirtha9 1 566 Jan-03-2021, 04:44 AM
Last Post: klllmmm
  LIST or ARRAY Comparison and change of value nio74maz 0 379 Dec-21-2020, 05:52 PM
Last Post: nio74maz
  Sume Every 10 element in the list and Divided of Sum quest_ 7 713 Nov-27-2020, 10:58 AM
Last Post: perfringo
  Counting Element in Multidimensional List quest_ 1 483 Nov-25-2020, 10:00 PM
Last Post: quest_
  Adding List Element if Second part of the List Elements are the Same quest_ 3 630 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Choose an element from multidimensional array quest_ 2 556 Nov-25-2020, 12:59 AM
Last Post: quest_
  Removing element from list squall 6 827 Nov-22-2020, 09:34 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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