python - 在字符串数组中搜索部分字符串

标签 python arrays python-3.x

在数组中搜索部分字符串然后从数组中删除该字符串的 Pythonic/快速方法是什么?
(我可以用一个简单的循环和 IF IN 来完成,并在循环中重建两个数组,询问是否有 Pythonic 方式/函数来做到这一点)

示例:

array = ['rule1','rule2','exception[type_a]','rule3','exception[type_b]']
res(,)=remove_exceptions(array,'exception')
print(res[0]) >>> ['rule1','rule2','rule3']
print(res[1]) >>> ['exception[type_a]','exception[type_b]']

最佳答案

>>> [x for x in array if 'exception' not in x]
['rule1', 'rule2', 'rule3']
>>> [x for x in array if 'exception' in x]
['exception[type_a]', 'exception[type_b]']

另请参阅:Python: split a list based on a condition?

关于python - 在字符串数组中搜索部分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39151056/

相关文章:

python - 拆开两列

python - 提高cython数组索引速度

javascript - React Reconciliation 是这样工作的吗?

c++ - 数组 (C++) : why use pointers over direct manipulation?

python - Pycharm 的终端不会更改 Project Interpreter 处的 Python 版本对应的 Python 版本

python - 使用 NaN 以外的填充值初始化 Pandas DataFrame

python - 通过许多子图改善子图大小/间距

javascript - 如何从javascript中的数组中删除空对象?

python - 在没有开始标签的情况下如何从汤中提取数据?

python - 如何找到理想的并行进程数以使用 python 多处理运行?