0

Answered in first comments, thanks all.

I have some of xy coordinate variables and I'm trying to populate a list with the names of those variables but having trouble.

Basically I need to create a loop that creates some X number of variableX and stick them inside a list and am struggling with that.

these are the coordinates lets say:

point1 = [-100, -100]
point2 = [1100, -100]
point3 = [-100, 1100]
point4 = [1100, 1100]

and this is what I'm trying to get without using a loop:

points = [
point1,
point2, 
point3, 
point4]

But since I'll have some number of points I'm at least trying to automate the second part (since the first one has unique coordinate which is harder to automate).

So my loop rn is:

#variables for the loop
num_points = 20
point_var = "point"
#loop to create list of points
for x in range(1, num_points + 1):
  point_var += ("%s" % x)
  points.append(point_var)
  point_var = "point"

And later I'm using this list in a function to populate points on a map, which works fine without the loop but doesn't work with the loop cause of a type error I'm getting

Traceback (most recent call last):
  File "D:\Projects\Voronoi-Map\Voronoi.py", line 101, in <module>
    vor = Voronoi(points)
  File "qhull.pyx", line 2597, in scipy.spatial.qhull.Voronoi.__init__
  File "D:\Software\Python\lib\site-packages\numpy\core\_asarray.py", line 177, in ascontiguousarray
    return array(a, dtype, copy=False, order='C', ndmin=1)
ValueError: could not convert string to float: 'point1'

So I'm kinda stuck here, the function expects floats but I converted the names of variables into strings in order to append them inside the loop. Is my approach just flat out wrong? I did something similar in java previously but don't know how to make it work here.

Any advice?

  • 1
    How did you end up with _n_ separate variables in the first place? This is where you would use a list of lists, i.e. `points = [[-100, -100], [1100, -100], [-100., 1100], [1100, 1100]]` then simply refer to them as `points[x]`. – Selcuk Dec 23 '20 at 04:16
  • 1
    `point_var` is a string. So `points` is a list of strings. You don't want a list of strings. Use numbers instead. – Tom Karzes Dec 23 '20 at 04:18
  • @TomKarzes that . is just an error, should all be integers – Chiril Russu Dec 23 '20 at 04:20
  • 1
    Whenever you find your program needs to use strings containing its own variable names in order to access its own data, something has gone wrong. If you have more than one of something, use a list or dict or other aggregate data structure instead of `thing0`, `thing1`, `thingN` which is going to result in a maintenance nightmare in no time. – ggorlen Dec 23 '20 at 04:20
  • "Basically I need to create a loop that creates some X number of variableX " no, you don't. You *never* need to dynamically create variables. Just make your list with the numbers you need. Making strings makes no sense to begin with – juanpa.arrivillaga Dec 23 '20 at 04:22
  • @ggorlen well I guess I don't know enough about these data structures to know if they solve my problem. can I tell a list to create N variables Var0, Var1 ... VarN? – Chiril Russu Dec 23 '20 at 04:22
  • @ChirilRussu You don't want N variables. You want N values, stored in a list. Individually naming them serves no purpose. – Tom Karzes Dec 23 '20 at 04:23
  • @ChirilRussu We don't either because you haven't really told us what problem you're trying to solve. See [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)--you're pretty much knee-deep into implementing something, got stuck and asked about Y, but there's not enough context to know how you got here in the first place or how all of this code is supposed to solve original problem X. All I can say is what is being attempted here won't work. – ggorlen Dec 23 '20 at 04:25
  • 1
    @Selcuk ok, this worked thanks. Guess I just didn't know how lists work. – Chiril Russu Dec 23 '20 at 04:26
  • 2
    @ChirilRussu *you don't need to create 10 variables* you need to create a list with ten items in it. Forget the variables – juanpa.arrivillaga Dec 23 '20 at 04:36

0 Answers0