1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Multi-Dimentional Arrays always result in the same pattern [duplicate]

Discussão em 'Python' iniciado por Stack, Setembro 13, 2024.

  1. Stack

    Stack Membro Participativo

    So I'm new to learning Python, and have been teaching myself while using it for a research project. I've been using Python code to help copy and reorganize large csv files and output them. My main field is in political science, and the bulk of my programming experience is as a hobbyist with other languages, mainly Java.

    So I keep running into an issue where after running my code, I look at the multidimensional arrays to store the data in Spyder ( or I'll occasional set the code to print out the whole array at specific points ), and I'll find each of the arrays are identical in content when they shouldn't be.

    So, to explain with an example, the dataset I'm working on right now contains a bunch of global elections data. The dataset has a unique id number for each party, so no two parties have the same value. I create an array to store this value : ( To help all of these examples are taken directly from my code, github link included at the bottom. )

    pMax = 220
    cMax = 200
    paid = [[ 0 ]*cMax]*pMax


    The code then goes through each line of the input file, and does the following bit of logic : ( Some Context: p and c are pointer variables for which party and country is being edited, pCode is the party id code from the file of the line currently being looked at, pX is an array of the total number of parties for each country, and prevPAID is the pCode value from the previous line )

    pCode = int( line[ 0 ] )
    if prevPAID != pCode :
    pFound = False
    for i in range( pMax ) :
    if pCode == paid[ i ][ c ] :
    p = i
    pFound = True
    if not pFound :
    p = pX[ c ]
    pX[ c ] += 1
    paid[ p ][ c ] = pCode


    The only other time I refer to this array is this line I added after the previous section while trying to troubleshoot this issue :

    paid[ p ][ c ] = pCode


    And yet when I run the code and look at the values of the array 'paid', which should contain no duplicates of any value, I see the same line of numbers in each index : 0, 809, 1739, 4892, 7255, 7544, 2309, 4285, 3694, 6113, 8063, 5161, ...

    Here's the Github link for the full code

    Okay, I guess what I expect to happen is I create an array to store the variable:

    array = [ [ 0 ]*3 ]*7
    array = [ [ 0, 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0, 0 ] ]


    And as I iterate through loops plugging in values, like say :

    z = 0
    for x in range( 3 ) :
    for y in range( 7 ) :
    array[ x ][ y ] = z
    z += 1


    I would expect the result to come out like so:

    array = [ [ 1, 2, 3, 4, 5, 6, 7 ],
    [ 8, 9, 10, 11, 12, 13, 14 ],
    [ 15, 16, 17, 18, 19, 20, 21 ] ]


    But instead what I actually get is :

    array = [ [ 1, 2, 3, 4, 5, 6, 7 ],
    [ 1, 2, 3, 4, 5, 6, 7 ],
    [ 1, 2, 3, 4, 5, 6, 7 ] ]


    I can't even figure out what I am doing wrong to get this result. Is it a failure of the logic that checks to see if the code has already been stored? Am I accidentally overwriting the data somehow?

    Continue reading...

Compartilhe esta Página