You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中NumPy二维数组元素重复次数、位置统计及行列对比需求

Solution for Tracking Element Occurrences and Distribution Analysis

Got it, let's work through this problem step by step to get the exact output you need, plus a detailed distribution breakdown. Here's how to approach it:

Step 1: Setup the Original Array

First, let's define the NumPy array we're working with:

import numpy as np

# Original 2D array
arr = np.array([
    [8, 12, 5, 2],
    [12, 15, 6, 10],
    [15, 8, 12, 5],
    [12, 15, 8, 6]
])

Step 2: Track Element Counts and Positions

We'll use a dictionary to map each unique element to its total occurrence count and 1-based positions (matching the format in your example). Here's the code to implement this:

# Dictionary to store element details: key = element value, value = (count, list_of_1based_positions)
element_tracker = {}

# Get unique elements and their total occurrence counts
unique_elements, total_counts = np.unique(arr, return_counts=True)

# For each element, find all its positions (converted to 1-based indices)
for elem, count in zip(unique_elements, total_counts):
    # Get 0-based indices where the element appears
    row_indices, col_indices = np.where(arr == elem)
    # Convert to 1-based positions (since your example uses 1-indexed rows/columns)
    positions = [(row + 1, col + 1) for row, col in zip(row_indices, col_indices)]
    element_tracker[elem] = (count, positions)

Step 3: Output in the Requested Format

To print the results in the syntax you provided:

# Print each element's info
for elem, (count, positions) in element_tracker.items():
    position_str = ", ".join([str(pos) for pos in positions])
    print(f"({elem}, {count}, {position_str})")

Running this code will produce:

(2, 1, (1, 4))
(5, 2, (1, 3), (3, 4))
(6, 2, (2, 3), (4, 4))
(8, 3, (1, 1), (3, 2), (4, 3))
(10, 1, (2, 4))
(12, 4, (1, 2), (2, 1), (3, 3), (4, 1))
(15, 3, (2, 2), (3, 1), (4, 2))

Step 4: Row and Column Distribution Analysis

Let's dive into the distribution using element 15 as a focused example (as you requested):

  • Total occurrences: 3
  • Exact positions: (2,2), (3,1), (4,2)
  • Row distribution: Appears in rows 2, 3, and 4 (one occurrence per row), with no presence in row 1. This means it's concentrated in the lower three-quarters of the array.
  • Column distribution: Shows up twice in column 2 (rows 2 and 4) and once in column 1 (row 3). It never appears in columns 3 or 4, so it's clustered on the left side of the array.

For a quick comparison, take element 12 (the most frequent element with 4 occurrences):

  • Row distribution: Present in all 4 rows (one per row)
  • Column distribution: Shows up twice in column 1, once in column 2, and once in column 3 — spread across the left three columns.

内容的提问来源于stack exchange,提问作者S. Waleed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:49:23