admin管理员组文章数量:1558098
如何在Python中从另一个数组中删除包含某些字符串的数组(How to remove an array containing certain strings from another array in Python)
例:
a = ['abc123','abc','543234','blah','tete','head','loo2']
所以我想从上面的字符串数组中筛选出以下数组b = ['ab','2']
我想从该列表中删除包含'ab'的字符串以及数组中的其他字符串,以便我得到以下内容:
a = ['blah', 'tete', 'head']
Example:
a = ['abc123','abc','543234','blah','tete','head','loo2']
So I want to filter out from the above array of strings the following array b = ['ab','2']
I want to remove strings containing 'ab' from that list along with other strings in the array so that I get the following:
a = ['blah', 'tete', 'head']
原文:https://stackoverflow/questions/17972025
更新时间:2019-11-09 04:56
最满意答案
您可以使用列表理解:
[i for i in a if not any(x in i for x in b)]
返回:
['blah', 'tete', 'head']
You can use a list comprehension:
[i for i in a if not any(x in i for x in b)]
This returns:
['blah', 'tete', 'head']
2013-07-31
相关问答
您可以使用列表理解: [i for i in a if not any(x in i for x in b)]
返回: ['blah', 'tete', 'head']
You can use a list comprehension: [i for i in a if not any(x in i for x in b)]
This returns: ['blah', 'tete', 'head']
只需使用带有按位not运算符的Array#filter()和Array#indexOf()进行检查。 ~是一个按位运算符 。 它非常适合与indexOf() ,因为如果找到索引0 ... n indexOf返回indexOf ,如果不是-1 , indexOf返回: value ~value boolean
-1 => 0 => false
0 => -1 => true
1 => -2 => true
2 => -3 => true
and so
...
你有没有尝试过 $tags = array(
"Free_Course_123",
"Free_Course_124",
"Other_Course_123",
"Other_Course_1234"
);
$cleanTags= preg_grep("/Free_Course/", $tags, PREG_GREP_INVERT);
print_r($cleanTags);
? 最后一个属性反转过滤器功能,请参见手册 我测试了结果,它应该从PHP 4.2.
...
Python不知道“数字”是什么意思。 if "a number":
搜索一个看起来像“一个数字”的字符串。 我知道的唯一方法(可能更多)是使用函数 if type(i) == :
print("*" * i)
这会向解释器询问我的类型(),并检查它是否与您正在查找的内容相同(==)。 Python doesn't know what "a number" means. if "a number":
searches for a string that
...
您可以使用ast.literal_eval >>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
You can use ast.literal_eval >>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(
...
我认为你的意思是你有一个字符串数组,它们都包含你想要删除的一些子字符串。 非破坏性: array.map {|s| s.gsub(keyword, '')}
根据需要使用破坏性变体在原地进行。 I think you mean you have an array of strings and they all contain some substring that you want to remove. Non-destructively: array.map {|s| s.gsub(keywo
...
您可以使用子字符串删除索引部分: for cmd in arr:
exec(cmd[5:])
You can remove the index part, by using substring: for cmd in arr:
exec(cmd[5:])
首先消除所有重复。 通过在迭代数据时使用哈希表并存储已经看过的字符串,这很容易做到。 (如果您害怕哈希表的最坏情况行为,您可以使用trie或sort并迭代来过滤掉dupe) 一旦筛选出所有重复项,请为所有剩余的字符串创建后缀树 。 在创建后缀树之后,对于每个字符串,检查它是否作为某个字符串的后缀而不是它本身。 这是通过遵循后缀树上从根到字符串结尾的路径来完成的,如果你唯一的选项是完全相同的字符串,那么它不是子字符串(否则 - 它是)。 时间复杂度: 过滤掉dupes:O(n * m) 理论上构建后
...
char数组和Python字符串的Python列表是两个非常不同的东西。 如果你想要一个包含char数组(一个字符串)的分支,那么我建议使用Python的内置bytearray类型: import ROOT
# create an array of bytes (chars) and reserve the last byte for null
# termination (last byte remains zero)
char_array = bytearray(21)
# all bytes
...
使用正则表达式模式 s = ["lorem", "ipsum", "1734", "dolor", "1", "301", "et", "4102", "92"]
s.reject { |l| l =~ /\A\d+\z/ }
# => ["lorem", "ipsum", "dolor", "et"]
Use a regexp pattern s = ["lorem", "ipsum", "1734", "dolor", "1", "301", "et", "4102", "92"]
s.r
...
版权声明:本文标题:删除array指定字符python_如何在Python中从另一个数组中删除包含某些字符串的数组(How to remove an array containing certain strings fr... 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1727340874a1109392.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论