← Back to NumPy Course | Chapter 2: Creating Arrays | Lesson 7 of 7

List/Tuple से Array बनाना

Lists और tuples दोनों arrays में convert होते हैं, और इन्हें nest करना rows और columns बनाता है।

In this page:

  1. List/Tuple से Array
Syntax
python
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

python
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. #}
आम गलतियां
  1. strings और numbers mix करना और एक string array पाना
  2. अलग lengths की rows देना
  3. यह मान लेना कि एक 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.