Jump to content

Confusion with 2D array

- - - - -

  • Please log in to reply
5 replies to this topic

#1
matija

matija

    Newbie

  • Members
  • Pip
  • 3 posts
I've just recently started learning python coming from C. Still trying to get my head around what's variable here and what's pointer so please be understanding :) . Here's my problem:

def create_table(m, n):

    matrix = [[1]*(m) for x in range(n)]            

    return matrix


def fill_table(m, n, table):

    

    pom = []

    pom.extend(table)

    print(pom is table) #just to be sure they aren't same object

    for i in range (m):        

        for j in range (n):

            pom[i][j] = j+1

    return pom


def main():


    table1=[]

    table1=create_table(4,4)

    table2=fill_table(4,4,table1)

    for i in range (4): print(table1[i])

    print("------------")

    for i in range (4): print(table2[i])




if __name__=="__main__": main()

So when I print these tables I get this:

False

[1, 2, 3, 4]

[1, 2, 3, 4]

[1, 2, 3, 4]

[1, 2, 3, 4]

------------

[1, 2, 3, 4]

[1, 2, 3, 4]

[1, 2, 3, 4]

[1, 2, 3, 4]

I don't understand how table1 gets filled with same data as table2, even though I don't manipulate with table1's anywhere and they aren't even same object? Or do I?

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
I don't really know why table1 has some values as table2 but why don't you create table inside fill_table function? Also, create_table is such an one liner :)

def create_table(m, n):

    return [[0]*m for x in range(n)]


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
matija

matija

    Newbie

  • Members
  • Pip
  • 3 posts
What I figured by now that table1 and table2 aren't same objects but fields in them are, and they are mutable maybe, not really sure but that's currently only logical. I'm thinking I'm taking wrong approach here, gonna go get a tutorial about vars, pointers and such in python.

Tnx for the one liner :) .

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Your tables have the same data due to shallow copying. The new pom list only maintains references to other lists, and those lists have been allocated with the first list, so if you modify list 2's elements (which are themselves lists), then you modify list 1's elements. Remember that while you call them "table", it doesn't mean that now they act like tables (see notes below), so extend will not copy the list's content lists when performing an extend. Instead you're going to have to do something like this:
pom = [[] for x in range(m)]

for x in range(m):

    pom[x].extend(table[x])
That, I believe, will work. Untested, of course.
Wow I changed my sig!

#5
matija

matija

    Newbie

  • Members
  • Pip
  • 3 posts
Best solution I got was this:

[[x for x in row] for row in table]

and this:

import copy

pom = copy.deepcopy(table)

I've tested your code and it works, but I find these two a bit more elegant so I thought I should share them. At least they work in py3.2, don't know for older versions.

#6
alma111

alma111

    Newbie

  • Members
  • Pip
  • 3 posts
hello

I have the following program..i am not getting how the program is using 2D array.May some body explain it in details.Thanks in advanced.

public void setData(double ad[][])

{

double ad1[][] = new double[ad.length][];

for(int i = 0; i < ad1.length; i++)

ad1 = ad[ad1.length - i - 1];//MY Problem is here

min = ad1[0][0];

max = ad1[0][0];

data = new float[ad1.length][];

for(int j = 0; j < ad1.length; j++)

{

data[j] = new float[ad1[j].length];

for(int l = 0; l < ad1[j].length; l++)

{

if(ad1[j][l] > max)

max = ad1[j][l];

if(ad1[j][l] < min)

min = ad1[j][l];

}

}

if(max == min)

{

for(int k = 0; k < ad1.length; k++)

{

for(int i1 = 0; i1 < ad1[k].length; i1++)

data[k][i1] = 1.0F;

}

} else

{

for(int j1 = 0; j1 < ad1.length; j1++)

{

for(int k1 = 0; k1 < ad1[j1].length; k1++)

{

Double double1 = new Double(1.0D - (ad1[j1][k1] - min) / (max - min));

data[j1][k1] = double1.floatValue();




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users