4

How to extract only the value from an array without the text "array" and "typecode"?

Array is shapely.linestring.centroid.xy:

a = LineString.centroid.xy
print(a)
>> (array('d', [-1.72937...45182697]), array('d', [2.144161...64685937]))
print(a[0])
>> array('d', [-1.7293720645182697])

I need only the -1.7293... as a float not the whole array business.

Georgy
  • 12,464
  • 7
  • 65
  • 73
Izak Joubert
  • 906
  • 11
  • 29

2 Answers2

6

In fact, individual coordinates of the Point can be accessed by x and y properties. And since object.centroid returns a Point, you can simply do:

>>> from shapely.geometry import LineString
>>> line = LineString([(0, 0), (2, 1)])
>>> line.centroid.x
1.0
>>> line.centroid.y
0.5

Additionaly, geometric objects such as Point, LinearRing and LineString have a coords attribute that returns a special CoordinateSequence object from which you can get individual coordinates:

>>> line.coords
<shapely.coords.CoordinateSequence at 0x7f60e1556390>
>>> list(line.coords)
[(0.0, 0.0), (2.0, 1.0)]
>>> line.centroid.coords[0]
(1.0, 0.5)
Georgy
  • 12,464
  • 7
  • 65
  • 73
1
print(a[0][0])

you are working with array inside array.

enter image description here

import array
a=(array.array('d',[-2.2,3,2,2]),array('d',[2,3,4]))
print(a[0][0])
Jainil Patel
  • 1,284
  • 7
  • 16