Government Security
Network Security Resources

Jump to content

Photo

Python Array Pass-By-Reference Help

- - - - - python
  • Please log in to reply
1 reply to this topic

#1 Banditrobber

Banditrobber

    Private

  • Members
  • 5 posts

Posted 31 January 2010 - 09:23 AM

I've been banging my head on the wall for about an hour now without being able to find an answer. I'm trying to create two multidimensional arrays and assign parts of one to different parts of another. I know Python does pass-by-reference automatically for variables set equal to each other, but this is a little different since the data will be arranged differently in the arrays. Let me give you a visual:

I've got a nine-by-nine multidimensional array like so:
['3', '0', '9', '0', '4', '7', '0', '0', '8']
['4', '7', '0', '0', '0', '0', '3', '0', '0']
['0', '0', '0', '0', '6', '0', '4', '0', '1']
['6', '0', '3', '4', '0', '1', '5', '8', '2']
['7', '1', '4', '5', '8', '2', '6', '9', '3']
['8', '2', '0', '0', '0', '3', '0', '1', '0']
['0', '0', '8', '0', '3', '6', '1', '0', '7']
['1', '0', '7', '8', '0', '5', '9', '3', '0']
['9', '3', '6', '0', '0', '4', '8', '2', '0']

and I want to create another nine-by-nine multidimensional array with the same data like so:
quad[0] = [grid9x9[0][0], grid9x9[0][1], grid9x9[0][2],
 	grid9x9[1][0], grid9x9[1][1], grid9x9[1][2],
 	grid9x9[2][0], grid9x9[2][1], grid9x9[2][2]]
That would only be the first row of the second array, but it should give you an idea of what I'm trying to do. By the way, if you can't tell what's happening there, I'm taking each 3x3 quadrant of the first array and assigning it to a row in the second array. In this case, I only did the first quadrant.

These two arrays are completely separate, but I want them linked together so that if I changed one, both would change.

Any ideas on how I could accomplish this in Python?

#2 Juza

Juza

    Specialist

  • Sergeant Major
  • 149 posts

Posted 31 January 2010 - 03:56 PM

I hope that explains all!

import unittest
import random

class ArrayTests(unittest.TestCase):
	def testArrays(self):
 	
 	array1 = [
 	['3', '0', '9', '0', '4', '7', '0', '0', '8'],
 	['4', '7', '0', '0', '0', '0', '3', '0', '0'],
 	['0', '0', '0', '0', '6', '0', '4', '0', '1'],
 	['6', '0', '3', '4', '0', '1', '5', '8', '2'],
 	['7', '1', '4', '5', '8', '2', '6', '9', '3'],
 	['8', '2', '0', '0', '0', '3', '0', '1', '0'],
 	['0', '0', '8', '0', '3', '6', '1', '0', '7'],
 	['1', '0', '7', '8', '0', '5', '9', '3', '0'],
 	['9', '3', '6', '0', '0', '4', '8', '2', '0']
 	]
	
 	array2 = array1
 	
 	print "Array1 id=", id(array1), " Array2 id=", id(array2)
 	
 	self.assertEquals(id(array1), id(array2), "Arrays1 and Array2 are equal?")
 	self.assertEquals(len(array1), len(array2), "Array Length")
 	
 	array3 = []
 	for a in range(9):
 	array3.append([random.randint(1, 9) for i in range(9)])
 	
 	array2 = array3

 	print "Array3 id=", id(array3), " Array2 id=", id(array2)
 	
 	self.assertEquals(id(array3), id(array2), "Arrays3 and Array2 are equal?")
 	self.assertEquals(len(array3), len(array2), "Array Length")
 	
 	print "Array3 id=", id(array3), " Array1 id=", id(array1)
 	
 	self.assertEquals(id(array3), id(array1), "Arrays3 and Array1 are equal?")
 	self.assertEquals(len(array3), len(array1), "Array Length")

if (__name__=='__main__'): 	
	unittest.main()

Output:

juza@Juza-Realm:~/Desktop$ python arrays.py 
Array1 id= 159623980 Array2 id= 159623980
Array3 id= 159624012 Array2 id= 159624012
Array3 id= 159624012 Array1 id= 159623980
F
======================================================================
FAIL: testArrays (__main__.ArrayTests)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "arrays.py", line 39, in testArrays
	self.assertEquals(id(array3), id(array1), "Arrays3 and Array1 are equal?")
Assertionerror: Arrays3 and Array1 are equal?

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (failures=1)

Go to iamjuza.blogspot.com
Follow me twitter.com/iamjuza

The true beginning of our end.






Also tagged with one or more of these keywords: python