← Back to NumPy Course | Chapter 5: Array Manipulation | Lesson 2 of 7

flatten()/ravel() का इस्तेमाल

दोनों एक multi-dimensional array को values की एक single flat line में बदल देते हैं।

In this page:

  1. flatten()/ravel()
Syntax
python
arr.flatten()    # always a copy
arr.ravel()      # view when possible

flatten()/ravel()

flatten हमेशा एक copy return करता है, जबकि ravel जब कर सके एक view return करता है। दोनों default से row-major order में elements पढ़ते हैं। जब आप result independently modify करने की योजना बनाएँ flatten चुनें।

Note: अगर आपको एक guaranteed independent array चाहिए, flatten या ravel().copy() इस्तेमाल करें।

उदाहरण: flatten()/ravel()

python
import numpy as np

m = np.array([[1, 2], [3, 4]])
f = m.flatten()
r = m.ravel()
f[0] = 99
print("original after flatten edit:", m[0, 0])
r[0] = 77
print("original after ravel edit:", m[0, 0])

# Output:
# original after flatten edit: 1
# original after ravel edit: 77
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. एक raveled view modify करना और unexpectedly original बदल देना
  2. यह मान लेना कि दोनों identical हैं
  3. default C row order भूल जाना
चैप्टर सारांश
  • flatten एक copy return करता है
  • ravel possible होने पर एक view return करता है
  • दोनों 1-D arrays produce करते हैं
  • Row-major order default है
🔒

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.