How to store a set of cooordinates , let us say 300 to array

Dani AI

Generated

For storing about 300 coordinate pairs in Python, choose the container that matches what you actually need to do with the points: simple storage/iteration, numeric/vector math, frequent mutation, spatial queries, or persistence. The thread already lists the common approaches; these notes expand on those options and fill gaps you might hit as the program grows.

For plain storage and iteration a list of 2-tuples is the simplest and most interoperable (easy to serialize or iterate). If you want named fields and clearer code, use a lightweight container type rather than raw tuples—this helps readability and type hints. If you plan to mutate points or attach methods/validation, a small class or dataclass is a good choice. Conversely, prefer immutability (NamedTuple or frozen dataclass) when you want safety. As and pointed out, avoid uncontrolled globals; pass the collection into functions or encapsulate it in a class to make data flow explicit.

When you need numeric performance or vectorized ops, store coordinates in a 2D numeric array instead of Python objects. For example, a dataclass for programmer-friendly code and a NumPy array for numeric work:

from dataclasses import dataclass

@dataclass
class Point:
    x: float
    y: float

points = [Point(x*0.1, x*0.2) for x in range(300)]
import numpy as np
points = np.zeros((300, 2), dtype=np.float32)
# fill, compute, or load data efficiently with NumPy functions

Practical tips: for small sets (300) pick the simplest structure that keeps the rest of your code clear. For larger sets or heavy math, prefer NumPy or typed arrays. For persistence choose CSV/JSON for portability or NumPy binary for numeric speed. If you need nearest-neighbour queries later, use a KD-tree implementation from common libraries. Tie the structure to the operations you actually perform rather than optimizing prematurely.

Recommended Answers

All 8 Replies

a =[300]

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

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.

or this.........

a = a= [(x+2,x*2) for x in range(1,300)]

And plot some graph with it ;)

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

create coordinate class :P

create coordinate class :P

Like :

from collections import namedtuple
Coordinates = namedtuple('Coordinates','x y')
c1 = Coordinates(23,34)
print(c1)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.