在数组中搜索部分字符串然后从数组中删除该字符串的 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 - 在字符串数组中搜索部分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39151056/