Port to Python

A forum for general discussion of the Python programming language.

Port to Python

Postby tleroy » Thu Jul 10, 2014 7:30 am

I am new to Python and would like help on the following:

I have a class and would like to create array of this class and initialize it. The
size of this array will be 3000 elements all of which will be pre-initalized.

For e.g.:

Code: Select all
class Coordinate:
    def __init__(self):
        self.x = 0
        self.y = 0

Marker[3000] = Coordinate ((33.4,12.344), (23.234, 64.344), (45.3434, 24.3344))

print Marker[20].a
print Marker[20].b

I get many compile errors and would like help to see if the above is the right way
to do.

Thanks
TLeroy
Last edited by Mekire on Thu Jul 10, 2014 7:40 am, edited 1 time in total.
Reason: First post lock. Code tags.
tleroy
 
Posts: 2
Joined: Thu Jul 10, 2014 7:16 am

Re: Port to Python

Postby Mekire » Thu Jul 10, 2014 7:50 am

Well, there are a lot of issues with the above code.
You don't pass your desired initialization variables to your __init__ being the first.

I'm afraid you will probably need to go through a basic tutorial to get up to speed.
You may also want to look into numpy eventually if speed is a concern (which it sounds like it might be).

In the meantime here is something to consider. It creates 3000 Coordinates with random x and y between 0-100:
Code: Select all
from random import uniform


class Coordinate(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return "<Coordinate({x:.3f}, {y:.3f})>".format(**vars(self))


marker = [Coordinate(uniform(0, 100), uniform(0, 100)) for _ in xrange(3000)]


print(marker[2855])
print(marker[5])
print(marker[5].x)
print(marker[5].y)
Output:
Code: Select all
<Coordinate(45.676, 6.519)>
<Coordinate(89.195, 74.566)>
89.1949434262
74.5664390595

Note the above is python 2.7 (use range instead of xrange for py3).
-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Port to Python

Postby tleroy » Thu Jul 10, 2014 1:00 pm

Thank you Mek for your suggestions. Yes, I need to get up to speed and thanks
for pointing to numpy. I will look at it. Initially, performance is not a concern.

>> It creates 3000 Coordinates with random x and y between 0-100:

I have predefined set of 3000 constant values which are not generated that need
to be assigned. I considered creating a empty list called "Marker" and use append
3000 times to assign x and y coordinate values. Instead of calling append, is
there any more elegant way? Sorry I come from the world of C.

Thanks once again.
TLeroy
tleroy
 
Posts: 2
Joined: Thu Jul 10, 2014 7:16 am

Re: Port to Python

Postby Mekire » Thu Jul 10, 2014 2:39 pm

I would have to see exactly what format your initial data is in, but essentially after your data is parsed into a list of (x,y) tuples, you would just do this:
Code: Select all
marker = [Coordinate(x,y) for x,y in loaded_data]

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 1 guest