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

如何使用Numpy更快筛选包含'u'或't'的字符串?

Using NumPy to Efficiently Filter Strings Containing 'u' or 't'

Absolutely! NumPy's vectorized string operations can be significantly faster than list comprehensions, especially when working with large datasets. Here's how you can implement this efficiently:

Step-by-Step Implementation

First, convert your list into a NumPy array of strings, then use NumPy's built-in string methods to create a boolean mask for filtering:

import numpy as np

# Your input list
fun_strings = ['abc','cat','but','cab','mug','xyz']

# Convert to a NumPy array of strings
np_strings = np.array(fun_strings)

# Create a mask for strings containing 'u' OR 't'
# Using regex with np.char.contains for concise syntax
mask = np.char.contains(np_strings, r'u|t', regex=True)

# Apply the mask to get filtered results (convert back to list if needed)
filtered_strings = np_strings[mask].tolist()

print(filtered_strings)  # Output: ['cat','but','mug']

Why This Is More Efficient

List comprehensions run a Python-level loop over each element, which adds overhead for every iteration. NumPy's string operations are vectorized—they’re implemented in optimized C code, handling the entire array in bulk without looping in Python. This difference becomes dramatic when working with thousands or millions of strings, where NumPy can outperform list comprehensions by a large margin.

Note for Small Datasets

If your input list is small (like the example here), you might not notice a speed difference. But as your dataset grows, NumPy’s approach will scale much better than a list comprehension.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:17:26