Lists की तुलना में NumPy क्यों
एक Python list कुछ भी hold कर सकती है लेकिन maths में slow है; एक NumPy array एक तरह का number hold करता है और एक साथ सब कुछ पर maths करता है।
In this page:
Lists से ज़्यादा NumPy क्यों
Python lists पूरे Python objects के pointers store करती हैं, इसलिए arithmetic को एक slow loop चाहिए। NumPy arrays memory के एक contiguous block में raw numbers store करते हैं और compiled code में operations apply करते हैं। इसका मतलब है कम memory, तेज़ maths, और कहीं छोटा code।
Note:
एक list को 2 से multiply करना इसे repeat करता है; एक array को 2 से multiply करना हर element double करता है।
उदाहरण: Why NumPy over lists
import numpy as np
nums = [1, 2, 3]
arr = np.array(nums)
print("list * 2 :", nums * 2)
print("array * 2 :", arr * 2)
print("list + list:", nums + nums)
print("array + array:", arr + arr)
# Output:
# list * 2 : [1, 2, 3, 1, 2, 3]
# array * 2 : [2 4 6]
# list + list: [1, 2, 3, 1, 2, 3]
# array + array: [2 4 6]
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. #}
आम गलतियां
- यह उम्मीद करना कि list * 2 values double करेगा
- types mix करना और automatic conversion से surprised होना
- बड़े numeric datasets के लिए lists इस्तेमाल करना
चैप्टर सारांश
- Arrays contiguous typed memory इस्तेमाल करते हैं
- Operators arrays पर element-wise काम करते हैं
- Arrays lists से कहीं कम memory इस्तेमाल करते हैं
- Code बिना explicit loops के छोटा है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: