NumPy字符串操作
diegosouzapw2026-10-27查看 1299复制 1718
使用 char 模块和现代字符串替代方案进行向量化字符串操作,包括清理和搜索操作。触发词:字符串操作、numpy.char、文本清理、子串搜索。
📘如何使用这个技能
- 1复制技能内容·点击上方「Copy SKILL.md」复制全部内容,或 Download 下载到本地
- 2打开你的 AI 客户端·支持 Claude Code、Trae、Cursor、ChatGPT、DeepSeek、Kimi 等任意 AI 助手
- 3粘贴为系统提示/知识文件·Claude Code:放入 .claude/skills/ 目录;普通对话:作为首条消息发送并说「请严格按这份技能文档执行」
- 4直接提出你的需求·AI 会按技能定义的工作流、格式和约束来处理,无需再解释背景
💡 提示:技能生效后,每次对话只需一句需求,AI 会按 SKILL.md 的流程执行。
SKILL.md3025 chars
--- name: numpy-string-ops description: Vectorized string manipulation using the char module and modern string alternatives, including cleaning and search operations. Triggers: string operations, numpy.char, text cleaning, substring search. --- ## Overview NumPy's `char` submodule provides vectorized versions of standard Python string operations. It allows for efficient processing of arrays containing `str_` or `bytes_` types, though it is being transitioned to a newer `strings` module in recent versions. ## When to Use - Cleaning large text datasets (e.g., stripping whitespace, normalization). - Performing batch substring searches across thousands of records. - Concatenating columns of text data using broadcasting. - Converting character casing for entire datasets simultaneously. ## Decision Tree 1. Starting new development? - Use `numpy.strings` if available; `numpy.char` is legacy. 2. Comparing strings with potential trailing spaces? - `numpy.char` comparison operators automatically strip whitespace. 3. Concatenating a constant prefix to an array of names? - Use `np.char.add(prefix, name_array)`. ## Workflows 1. **Batch String Concatenation** - Create two arrays of strings, A and B. - Use `np.char.add(A, B)` to join them element-wise. - Broadcasting applies if one array is a single string and the other is multidimensional. 2. **Cleaning Text Datasets** - Identify an array of messy text. - Apply `np.char.strip(arr)` to remove whitespace. - Use `np.char.lower(arr)` to normalize casing across the entire dataset. 3. **Finding Substrings in Arrays** - Use `np.char.find(text_array, 'target_word')`. - Identify elements with non-negative indices (where the word was found). - Filter the original array using boolean indexing based on the search result. ## Non-Obvious Insights - **Legacy Status:** The `char` module is considered legacy; future-proof code should look towards the `numpy.strings` alternative. - **Implicit Stripping:** Unlike standard Python `==`, `char` module comparison operators strip trailing whitespace before evaluating equality. - **Vectorization Reality:** While these operations are vectorized, string manipulation is inherently less performant than numeric math because strings have variable lengths and require more complex memory management. ## Evidence - "Unlike the standard numpy comparison operators, the ones in the char module strip trailing whitespace characters before performing the comparison." [Source](https://numpy.org/doc/stable/reference/routines.char.html) - "The numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_ or numpy.bytes_." [Source](https://numpy.org/doc/stable/reference/routines.char.html) ## Scripts - `scripts/numpy-string-ops_tool.py`: Routines for batch text cleaning and search. - `scripts/numpy-string-ops_tool.js`: Simulated string concatenation logic. ## Dependencies - `numpy` (Python) ## References - [references/README.md](references/README.md)