<div dir="ltr"><div>Allan thank you for your draft! I agree with you that  (not in mine ) in general case it would be hard to resolve all corner cases. Also I think if someone read numpy reference linearly, he/she will have some insight that list of tuples are necessary (but it was not my case). <br><br>For me one problem is that in some cases numpy allows a lot freedom, but in other it is unnecessarily strict. Another one is exception messages (<span lang="en">but this is certainly subjective</span>). <br><br></div><br><div><div><div class="gmail_extra"><br><div class="gmail_quote">2017-03-24 19:48 GMT+03:00 Allan Haldane <span dir="ltr"><<a href="mailto:allanhaldane@gmail.com" target="_blank">allanhaldane@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="gmail-HOEnZb"><div class="gmail-h5">On 03/23/2017 02:16 PM, Kirill Balunov wrote:<br>
> It was the first time I tried to create a structured array in numpy.<br>
> Usually I use pandas for heterogeneous arrays, but it is one more<br>
> dependency to my project.<br>
><br>
> It took me some time (really, much more than some), to understand the<br>
> problem with structured array creation. As example:<br>
><br>
> I had list of list of this kind:<br>
> b=[[ 1, 10.3, 12.1, 2.12 ],...]<br>
><br>
> And tried:<br>
> np.array(b, dtype='i4,f4,f4,f4')<br>
><br>
> Which raises some weird exception:<br>
> TypeError: a bytes-like object is required, not 'int'<br>
><br>
> Two hours later I found that I need list of tuples. I didn't find any help<br>
> in documentation and could not realize that the problem with the inner<br>
> lists...<br>
><br>
> Why there is such restriction - 'list of tuples' to create structured<br>
> array? What is the idea behind that, why not list of lists, or tuple of<br>
> lists or ...?<br>
><br>
> Also the exception does not help at all...<br>
> p.s.: It looks like that dtype also accepts only list of tuples. But I can<br>
> not catch the idea for this restrictions.<br>
><br>
<br>
</div></div>The problem is that numpy needs to distinguish between multidimensional<br>
arrays and structured elements. A "list of lists" will often trigger<br>
numpy's broadcasting rules, which is not what you want here.<br>
<br>
For instance, should numpy interpret your input list as a 2d array of<br>
dimension Lx4 containing integer elements, or a 1d array of length L of<br>
structs with 4 fields?<br>
<br>
In this particular case maybe numpy could, in principle, figure it out<br>
from what you gave it by calculating that the innermost dimension is<br>
the same length as the number of fields. But there are other cases (such<br>
as assignment) where similar ambiguities arise that are harder to<br>
resolve. So to preserve our sanity we want to require that structures be<br>
formatted as tuples all the time.<br>
<br>
I have a draft of potential updated structured array docs you can read here:<br>
<a href="https://gist.github.com/ahaldane/7d1873d33d4d0f80ba7a54ccf1052eee" rel="noreferrer" target="_blank">https://gist.github.com/<wbr>ahaldane/<wbr>7d1873d33d4d0f80ba7a54ccf1052e<wbr>ee</a><br>
<br>
See the section "Assignment from Python Native Types (Tuples)", which<br>
hopefully better warns that tuples are needed. Let me know if you think<br>
something is missing from the draft.<br>
<br>
(WARNING: the section about multi-field assignment in the doc draft is<br>
incorrect for current numpy - that's what I'm proposing for the next<br>
release. The rest of the docs are accurate for current numpy)<br>
<br>
Agreed that the error message could be changed.<br>
<br>
Allan<br>
<br>
______________________________<wbr>_________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/numpy-<wbr>discussion</a><br>
</blockquote></div><br></div></div></div></div>