0

So what I have is a list containing floats stored like so:

points = [(0.06 , -4.00), (3.76, 0.02), (7.53, 0.09), (26.28, 1.15)]

So index[0] == (0.06, -4.00)

and I would like to pass them one by one to a function that accepts parameters in the format

Point(x,y)

so at first I thought I had a solution with

for item in points:
    p = Point(item)

I quickly realised that is really only providing the function with

Point((0.06, -4.00))

which leaves the function wanting one more parameter, seeing as it thinks that is only the 'x' variable. I have seen string stripping, but I can't seem to convert the indices of Points back to float after I am done stripping. I think it may be due to the comma interfering.

Some help or hints would be appreciated!

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Xavier Dass
  • 575
  • 1
  • 9
  • 25

3 Answers3

6

You can do:

for x, y in points:
    p = Point(x, y)

or this:

for item in points:
    p = Point(*item)
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
1

points[0] is equal to (0.06 , -4.00), so points[0][0] is equal to 0.06 and points[0][1] is equal to -4.00.

for item in points:
    p = Point(item[0],item[1])
ForceBru
  • 43,482
  • 10
  • 63
  • 98
1

You can use unpacking here.

for item in points:
    p = Point(*item)
pvncad
  • 177
  • 1
  • 1
  • 8