List/Tuple से Array बनाना
Lists और tuples दोनों arrays में convert होते हैं, और इन्हें nest करना rows और columns बनाता है।
In this page:
Syntax
arr = np.array((value1, value2, value3))
matrix = np.array([(a, b), (c, d)])
List/Tuple से Array
np.array कोई भी sequence accept करता है, tuples, tuples की lists, और mixed nested sequences समेत। np.asarray possible होने पर बिना copy किए same करता है। np.fromiter किसी भी iterable से एक array बनाता है।
Note:
Ranges के लिए np.array(list(range(5))) या बस np.arange(5) इस्तेमाल करें।
उदाहरण: Array from list/tuple
import numpy as np
from_list = np.array([1, 2, 3])
from_tuple = np.array((4, 5, 6))
grid = np.array([(1, 2), (3, 4)])
mixed = np.array([1, "a"])
print(from_list, from_tuple)
print(grid)
print(mixed, mixed.dtype)
# Output:
# [1 2 3] [4 5 6]
# [[1 2]
# [3 4]]
# ['1' 'a'] <U21
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. #}
आम गलतियां
- strings और numbers mix करना और एक string array पाना
- अलग lengths की rows देना
- यह मान लेना कि एक tuple tuple रहेगा
चैप्टर सारांश
- Lists और tuples equally convert होते हैं
- Nested sequences 2-D arrays देते हैं
- asarray needless copies avoid करता है
- Mixed strings और numbers strings बन जाते हैं
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: