0

I'm working on an application that gets a list of x and y coordinates from various data sources. Depending on which data sources I need to go to (which is outside my control), there are multiple ways the data is returned.

  • an array of x and y values - [[x1,x2,x3...],[y1, y2, y3...]]
  • an array of x,y pairs - [[x1,y1],[x2,y2], ...]
  • a dictionary of x:y values - {x1:x2, x2:y2, x3:y3, ...}
  • a numpy array of x and y values - array([[x1,x2,x3...],[y1, y2, y3...]])
  • a numpy of x,y pairs - array([[x1,y1],[x2,y2], ...])

Using isinstance and len/shape I know I can solve this problem with a switch statement. I was wondering if there was a better way to convert these to a consistent format, or to read these in a consistent way.

2 Answers 2

1

There is no way to treat these structures identically (they aren't all ducks).
However, it's fairly easy to convert into a consistent format, make your pick and convert into that format, e.g.

zip(*[[x1,x2,x3...],[y1,y2,y3...]]) == [(x1,y1),(x2,y2),(x3,y3)...]
zip(*[[x1,y1],[x2,y2],[x3,y3]...]) == [(x1,x2,x3...),(y1,y2,y3...)]
list({x1:x2, x2:y2, x3:y3, ...}.items()) == [(x1,y1),(x2,y2),(x3,y3)...]
dict([(x1,y1),(x2,y2),(x3,y3)...]) = {x1:x2, x2:y2, x3:y3, ...}

Numpy arrays can largely be treated the same as Python lists.

Sign up to request clarification or add additional context in comments.

Comments

0

Adding to achampion's conversions:

You can construct a dictionary from a list of pairs:

dict([[x1,y1],[x2,y2],...]) == {x1:x2, x2:y2, x3:y3, ...}

np.asarray can convert lists to arrays, and happily passes an array through unchanged. numpy functions often pass their inputs through asarray (or a variant) to insure that the inputs are arrays of known dimension.

And array transpose is the equivalent of that list zip(*...).

np.asarray([[[x1,x2,x3...],[y1,y2,y3...]]) == np.asarray([(x1,y1),(x2,y2),(x3,y3)...]).T

I don't think you can go from dictionary to array without an intermediary list. But a (n,2) array works just as well as a list of pairs as input to dict().

So it is easy to convert one layout to another, but I don't think there is one function that does it for all of your layouts.

Is any one of these layouts more convenient for further processing?

Pseudo code might be:

if x is a dictioary:
   x = convert_to_list(x)
x = asarray(x)
if x.shape[0]==2 and x.shape[1]>2:
    x = x.T
# [[1,2],[3,4]] is ambiguous

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.