1

I have these coordinates of a bounding box in this format: (51.901071, 7.543488, 52.002215, 7.692406) and I want to convert it into a list of coordinates so that I can iterate through the pair. The final output should be like this: [(51.901071, 7.543488), (52.002215, 7.692406)] How can I convert the coordinates into a list?

closed as off-topic by user2856, Vince, Kadir Şahbaz, Jochen Schwarze, Mark Ireland Sep 10 '18 at 14:19

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – Vince, Mark Ireland
  • "Questions relating to general IT or with no clear GIS component, are off-topic here but can be researched/asked at Stack Overflow (software development), Super User (computing hardware and software), Database Administrators (relational databases) and other SE sites" – user2856, Kadir Şahbaz, Jochen Schwarze
If this question can be reworded to fit the rules in the help center, please edit the question.

2

Use list slicing with a step to extract the x and ys as separate tuples and the zip function to put them into a list of tuples.

>>> coords = (51.901071, 7.543488, 52.002215, 7.692406)
>>> coords[0::2]  # extract every 2nd element starting from 0 (the first element)
(51.901071, 52.002215)
>>> coords[1::2]  # extract every 2nd element starting from 1 (the 2nd element)
(7.543488, 7.692406)
>>> zip(coords[0::2],coords[1::2])
[(51.901071, 7.543488), (52.002215, 7.692406)]

Note In python 3, the output of zip is a generator rather than a list.

  • What would be the disadvantage of using: [coords[0::2],coords[1::2]] over zip(coords[0::2],coords[1::2])? This would avoid the differing behavior between versions, no? – Hayden Elza Sep 10 '18 at 13:31
  • The behaviour between versions isn't different, a generator is like a list, but doesn't get pre-built in memory, each element is generated as it's required. Your suggestion would give you [[x, x, x, etc], [y, y, y, etc]] Completely different and not as useful – user2856 Sep 10 '18 at 18:06
  • Oops, I should have written [coords[:2],coords[2:]]. I think my point still stands for this bounding box case, but yes you are right, your example is expandable for longer lists of coordinates. Thanks for your feedback, your zip example taught me something new. – Hayden Elza Sep 10 '18 at 18:13
  • For a two pair bounding box, your example will work fine. It will be slightly more efficient. – user2856 Sep 10 '18 at 21:20
  • And by "slightly more efficient" I mean it takes ~2/3 the time - roughly 0.0015 secs faster for 10,000 iterations of each on my PC (in total, not per iteration). – user2856 Sep 11 '18 at 2:08
0

Assign the left part of the coordinate to a key, and the right part to another key (Here it is 1st and 3rd to one key and 2nd and 4th to another key), in the Python list or Python dict object.

After that, you will have more control to access it via some function calls.

An alternative is to use numpy.reshape(), which will do this task trivially.

Not the answer you're looking for? Browse other questions tagged or ask your own question.