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

如何重新定义int64数组变量:筛选有效值并排序

Solution to Filter and Sort Valid Concentration Values

Got it, let's work through this to get the exact result you're looking for. Here are two approaches—one that builds on your existing code, and a more streamlined version that cuts out unnecessary steps.

Approach 1: Build on Your Existing Code

First, let's fix a small variable naming conflict in your original code, then add the filtering and sorting step:

import numpy as np

# Original raw data
data = ['10 20 10 36 30 33 400 400 -1 -1', 
        '100 50 50 30 60 27 70 24 -2 -2 700 700', 
        '300 1000 80 21 90 18 100 15 110 12 120 9 900 900 -3 -3', 
        '30 90 130 6 140 3 -4 -4 1000 1000']

# Split each string into individual elements
data_split = [e.split() for e in data]

# Extract concentration values (every 2nd element starting at index 3) and convert to numpy arrays
concentration = [np.array(row[3::2], dtype=np.int) for row in data_split]

# Step 1: Mark out-of-range values as 0 (your existing logic)
for row_idx in range(len(concentration)):
    for elem_idx in range(len(concentration[row_idx])):
        val = concentration[row_idx][elem_idx]
        if not (0 <= val <= 50):
            concentration[row_idx][elem_idx] = 0
            print(f"Error: Index {elem_idx} in row {row_idx} is out of range")

# Step 2: Filter out 0s and sort the valid values
concentration_clean_sorted = [np.sort(arr[arr != 0]) for arr in concentration]

# Print the final result
for arr in concentration_clean_sorted:
    print(f"Array of int64 {arr}")

How This Works:

  • We use numpy boolean indexing (arr != 0) to quickly filter out all invalid 0 values from each array.
  • np.sort() sorts the remaining valid values in ascending order in one line—far cleaner than writing nested loops for sorting.

Approach 2: Streamlined (No Intermediate 0 Step)

If you want to skip marking values as 0 entirely and go straight to filtering valid values, this version is more efficient:

import numpy as np

data = ['10 20 10 36 30 33 400 400 -1 -1', 
        '100 50 50 30 60 27 70 24 -2 -2 700 700', 
        '300 1000 80 21 90 18 100 15 110 12 120 9 900 900 -3 -3', 
        '30 90 130 6 140 3 -4 -4 1000 1000']

data_split = [e.split() for e in data]
concentration_clean_sorted = []

for row in data_split:
    # Extract and convert concentration values to numpy array
    arr = np.array(row[3::2], dtype=np.int)
    
    # Filter values that are within 0-50 range
    valid_vals = arr[(arr >= 0) & (arr <= 50)]
    
    # Sort the valid values
    sorted_vals = np.sort(valid_vals)
    concentration_clean_sorted.append(sorted_vals)
    
    # Print error messages for out-of-range indices (optional)
    invalid_indices = np.where((arr < 0) | (arr > 50))[0]
    for idx in invalid_indices:
        print(f"Error: Index {idx} in current row is out of range")

# Output the final sorted arrays
for arr in concentration_clean_sorted:
    print(f"Array of int64 {arr}")

Why This Is Better:

  • It combines validation, filtering, and sorting into a single pass, avoiding the need to modify the original arrays with 0s first.
  • Uses numpy's vectorized operations which are faster than nested Python loops, especially for larger datasets.

Final Output

Both approaches will give you exactly the result you want:

Array of int64 [33 36]
Array of int64 [24 27 30]
Array of int64 [12 15 18 21]
Array of int64 [3 6]

内容的提问来源于stack exchange,提问作者Kimdey

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:24:30