More realistically you get the cordinates from some file and you do split for each line and convert strings to numbers:

coordinate_matrix = [map(float, line.strip().split(',')) for line in open(coordinate_file_name) if line.strip()]

if you use it in a different part of your program you should also make it a global

There's a whole semi-religious argument about globals. (Java, for instance doesn't allow them at all, sort of, whereas in some early versions of basic, all variables were global). There are basically two ways to cope with much-used data:

  1. Make it global
    • It is easy to view (in Python), but requires you to declare it global if a function needs to alter it
    • In the presence of threads, there are potential race conditions or serialization through locking
    • It is somewhat harder to reason about what the program does because you don't know exactly which other functions may touch it without examining the entire program.
  2. Make it local and pass it around as argument to functions that need it
    • It is a little harder to deal with because functions have to accept it as a parameter, and calling code needs to pass it
    • In the presence of threads it is easier to prevent race conditions: Either pass a copy or pass a locking object along with it.
    • It is easier to reason about which functions have access to it: They name it in their parameter lists

As a general rule, I think that global data is if not downright bad, at least a little distasteful; so I oppose a generic broad-brush 'Make your data global'. And bear in mind that Python passes arrays by reference, so there is small cost to passing an array to a function.

You could store it as a list of (x, y) or (x, y, z) tuples.
I think richieking already gave you the hint.

Edited 5 Years Ago by vegaseat: n/a

create coordinate class :P

Like :

from collections import namedtuple
Coordinates = namedtuple('Coordinates','x y')
c1 = Coordinates(23,34)
print(c1)
This article has been dead for over six months. Start a new discussion instead.