Structured Arrays क्या हैं
एक structured array एक छोटी table की तरह काम करता है: हर element की named fields हैं शायद अलग types की।
In this page:
Syntax
dt = np.dtype([("field1", type1), ("field2", type2)])
arr = np.array([(value1, value2)], dtype=dt)
arr["field1"]
Structured Arrays
एक compound dtype को (name, type) pairs की एक list की तरह define करें। हर field नाम से access होती है, और array fast और compact रहता है। Structured arrays छोटे heterogeneous records के लिए suit करते हैं; बड़े tabular work के लिए Pandas इस्तेमाल करें।
Note:
Records को एक field से sort करने के लिए order="field" के साथ np.sort इस्तेमाल करें।
उदाहरण: Structured arrays
import numpy as np
people = np.array(
[("Ann", 31, 55.5), ("Bob", 25, 70.2)],
dtype=[("name", "U10"), ("age", "i4"), ("weight", "f4")],
)
print(people["name"])
print(people[1])
print(np.sort(people, order="age")["name"])
# Output:
# ['Ann' 'Bob']
# ('Bob', 25, 70.2)
# ['Bob' 'Ann']
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- यह भूल जाना कि string fields को U10 जैसा एक fixed size चाहिए
- field access को indexing से mix up करना
- जब Pandas एक बेहतर fit हो structured arrays के लिए reach करना
चैप्टर सारांश
- dtype (name, type) pairs की एक list है
- Fields को नाम से access करें
- हर record types mix कर सकती है
- order के साथ sort करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: