← Back to NumPy Course | Chapter 4: Indexing & Slicing | Lesson 5 of 7

2D Array की Slicing

एक sub-table निकालने के लिए एक साथ rows और columns slice करें।

In this page:

  1. Slicing 2D
Syntax
python
arr[row_start:row_stop, col_start:col_stop]
arr[:, col]
arr[row, :]

Slicing 2D

commas से separated प्रति axis एक slice दें: arr[rows, cols]। एक अकेला colon उस axis पर सब कुछ मतलब है। यह rows, columns, या rectangular blocks extract करना आसान बनाता है।

Note: m[:, 1] पूरा second column select करता है।

उदाहरण: Slicing 2D

python
import numpy as np

m = np.arange(1, 13).reshape(3, 4)
print(m)
print("column 1:", m[:, 1])
print("first two rows, last two cols:")
print(m[:2, 2:])

# Output:
# [[ 1  2  3  4]
#  [ 5  6  7  8]
#  [ 9 10 11 12]]
# column 1: [ 2  6 10]
# first two rows, last two cols:
# [[3 4]
#  [7 8]]
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. पूरी axis के लिए colon भूल जाना
  2. row और column slices mix up करना
  3. एक slice modify करना और यह भूल जाना कि यह एक view है
चैप्टर सारांश
  • arr[row_slice, col_slice] इस्तेमाल करें
  • एक colon मतलब सब कुछ
  • m[:, j] एक column है
  • Results views हैं
🔒

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.