1

What's the best way to communicate array values as part of a minimal example in a python question on StackOverflow or Github? R has the function dump(), which produces a text representation of an object that can then be loaded back into an R session using source(). Two lines of R code and someone else has access to the numbers/object/whatever I'm working with.

I want to raise an issue at the holoviews/geoviews GitHub repo about some problems I'm having making regional plots of gridded data. I suspect that the problem is partially related to nature of my grid, which is a 36x36 irregularly-spaced array of latitudes and longitudes.

Since it's not that many points I'm thinking it's probably easiest to simply include these coordinates in my question/issue. I can probably figure out something with fromstring() and reshape(). I wondered if there's a more concise way to do it.

  • Many questions just do something like `arrayname = np.array([1, 2, 3])` – Barmar Aug 28 '19 at 21:06
  • Or `arrayname = np.zeros((2, 3))` – Barmar Aug 28 '19 at 21:07
  • `np.savetxt()` and `np.loadtxt()` https://stackoverflow.com/a/28440249/2836621 – Mark Setchell Aug 28 '19 at 21:37
  • For modest sized arrays, the `repr` display is useful. `'array([0, 0, 3])'`. It looks a lot like the code that is used to create an array from a (nested) list. Browse other `numpy` questions, and pay attention to the answers. A number of higher-ranked posters copy-n-paste their answer from `ipython` sessions, showing both input and output code. – hpaulj Aug 28 '19 at 21:44

1 Answers1

0

This existing answer shows how to use json.dumps() to serialise to a string, although as numpy arrays are not directly serialisable, they need to be converted to a list first:

https://stackoverflow.com/a/51836953/3592211

Michael MacAskill
  • 2,411
  • 1
  • 16
  • 28