Jun-08-2020, 12:06 AM
Another numpy ndarray question: If I have a class value as the value of each numpy.ndarray element, how do I print the values of the embedded objects?
Example code and console output follows. What I would like to see is the same kind of output from printing elements of g.q as I can print from g.s.
OS is Win10-64 Pro, Python version is 3.8.3.
Peter
Example code and console output follows. What I would like to see is the same kind of output from printing elements of g.q as I can print from g.s.
OS is Win10-64 Pro, Python version is 3.8.3.
#!/usr/bin/env python import numpy as np class Q: def __init__(self, qsize, sval): self.s = np.full([qsize], sval, dtype=(str,3)) class G: def __init__(self, gsize, qval): self.m = np.ones([gsize], np.int32) self.q = np.full([gsize], qval, dtype=Q) q = Q(3, "abc") for x in q.s: print(x) g = G(3, q) for x in g.m: print(x) for x in g.q: print(x) exit()
Output:abc
abc
abc
1
1
1
<__main__.Q object at 0x000001E0960AACA0>
<__main__.Q object at 0x000001E0960AACA0>
<__main__.Q object at 0x000001E0960AACA0>
Thanks in advance for helping to cure more of my python ignorance.Peter