2

I'm trying to write out the coordinates of a 3d object, but I'm not sure how to list it.

so far I have:

class threed:
    def __init__(self,length,width,height):
        self.h =height
        self.l = length
        self.w = width
        self.f=0
        self.lwh = (length,width,height)

for i in range(1,3):
      for j in range(1,3):
           for k in range(1,3):
                coordinates=threed(i,j,k)

The problem is my function rewrites the variable coordinates each time so that I can't ever access coordinate (1,1,1) for example.

Note the above is for a 2x2x2 object.

How do I write it efficiently so that I can reference any coordinate as I need?

3
  • Is the second self.h supposed to be self.w? And do you mean you want a multidimensional array? If it's in a multidimensional array, why even bother, since you can just create a new class instance based on the position you would have fetched it in in the array? If it's not a multidimensional array, what are you trying to do? Commented Jul 30, 2012 at 1:35
  • What are you really trying to do? Other than the typo @minitech mentions, there's nothing wrong with your threed class other than the fact that it doesn't really do anything. Your loops don't accomplish much, but it's not clear what you're aiming for. Commented Jul 30, 2012 at 1:38
  • Sorry, yea the self.h for the second one should be self.w. @Blcknght I'm trying to write a program that fills a container, so the reason I need a class is basically to reference .f and see if it's empty or not. Commented Jul 30, 2012 at 1:42

2 Answers 2

5

You can do:

[threed(*x) for x in itertools.product(range(1, 3), range(1,3), range(1,3))]

to have each object. You may also use 3 for loops as well:

[threed(x, y, z) for x in range(1, 3) for y in range(1,3) for z in range(1,3)]

Adding the __repr__ method to your class, you can see more easily the result.

def __repr__(self):
    return 'threed' + repr(self.lwh)

So the output of the first code will be:

[threed(1, 1, 1), threed(1, 1, 2), threed(1, 2, 1), threed(1, 2, 2), 
 threed(2, 1, 1), threed(2, 1, 2), threed(2, 2, 1), threed(2, 2, 2)]

To allow threed objects to be compared, you can add a __eq__ method:

def __eq__(self, other):
    return self.lwh == other.lwh
Sign up to request clarification or add additional context in comments.

4 Comments

is there a way that I can reference the elements of that list by referencing the coordinates, for example if I type threed(1,1,1) in that list, it will say False
@Chowza That's because you haven't wrote a method to make your threed objects comparable.
How would I write a method to make it comparable?
@Chowza read the last part of the answer I just wrote. Be sure to add the return I forgot when writing
1

After coordinates=threed(i,j,k) you can add coordinates to a dictionary, for efficient access later.

Something like:

obj3d = dict()
for i in range(1,3):
      for j in range(1,3):
           for k in range(1,3):
                key = (i, j, k)
                obj3d[key] = threed(*key)

Then you have a obj3d dictionary, and can do:

  any_threed = obj3d[(1,1,1)]

Of course, you can use itertools etc. to tidy up the way you generate the coordinates, but I focused this answer on getting an efficient method of accessing the coordinates later.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.