简体   繁体   中英

how to split tuple and assign to new variables python

I have the following code to get a shapefile's layer extent in python which prints the extent as (317044.25, 322287.25, 681703.75, 685053.25)

from osgeo import ogr

# Get a Layer's Extent
inShapefile = "c:/shapefile.shp"
inDriver = ogr.GetDriverByName("ESRI Shapefile")
inDataSource = inDriver.Open(inShapefile, 0)
inLayer = inDataSource.GetLayer()
extent = inLayer.GetExtent()
featureCount = inLayer.GetFeatureCount()

print(extent)

I'd now like to do a few things to this tuple:

  • Separate them into 2 new variables
  • Remove the decimal places
  • Remove the brackets

Therefore leaving my results as follows:

extent1 = 317044, 322287
extent2 = 681703, 685053

I've tried the following code to split the tuple, but can't figure out how to assign to new variables and do the other requirements

# tuple into 2 tuples
res = tuple(extent[x:x + 2] 
      for x in range(0, len(extent), 2))
  
# printing result
print (str(res))

Separating tuple s into variables

Sequence unpacking and tuple packing ?

from osgeo import ogr

## Get a Layer's Extent
inShapefile = "c:/shapefile.shp"
inDriver = ogr.GetDriverByName("ESRI Shapefile")
inDataSource = inDriver.Open(inShapefile, 0)
inLayer = inDataSource.GetLayer()

## Unpack
x_min, x_max, y_min, y_max = inLayer.GetExtent()
# (317044.25, 322287.25, 681703.75, 685053.25)

## Pack
extent1 = x_min, x_max
# (317044.25, 322287.25)
extent2 = y_min, y_max
# (681703.75, 685053.25)

Removing decimal place

If you want to round the floats, you can wrap the variables in int() :

extent1 = int(x_min), int(x_max)
# (317044, 322287)
extent2 = int(y_min), int(y_max)
# (681703, 685053)

You can also use round(x[, n]) , math.floor(x) , math.ceil(x) depending on what your needs are.

PS You may examples from Python GDAL/OGR Cookbook of use.

Removing brackets

My hunch is you may be mistaking these tuple / sequence operations for string outputs. I don't believe you intend to be doing this.

However, lets say you really wanted to coalesce a tuple like (317044, 322287) into a str object that printed like 317044 322287 :

extent1_str = " ".join([str(item) for item in extent1])
# '317044 322287'
extent2_str = " ".join([str(item) for item in extent2])
# '681703 685053'

For future reference, we'd say "317044 322287" (in single or double quotes) which would imply we're referring to textual data (aka "string literal"). These look like magic, but in reality they're computed by "dunder" methods on the object: __str__

The question is bit unclear, But I assume that you want to convert decimle to integer and assign it to two varibles
Hers's the code that might solve your problem.

extent=(317044.25, 322287.25, 681703.75, 685053.25)
extent = list(map(int,extent))

extent1=extent[:2]
extent2=extent[2:]

print(extent1,extent2)

The output:

[317044, 322287] [681703, 685053]

To print according to your requirement:

print('extent1 = {},{}'.format(extent1[0],extent1[1]))
print('extent2 = {},{}'.format(extent2[0],extent2[1]))

Output:

extent1 = 317044,322287
extent2 = 681703,685053

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM