[英]faster way to do grep -v in python
i have two list , a = ["1","3","4","22","2"]
and b = ["1","2"]
我有两个列表,
a = ["1","3","4","22","2"]
和b = ["1","2"]
is there a way to get the output c = ["3","4"]
in python, just like有没有办法在python中获得输出
c = ["3","4"]
,就像
grep -vf b a
in bash if you have file a在 bash 中,如果您有文件 a
1
3
4
22
2
and b和 b
1
2
i cannot using grep , and i tried this , but it takes a long time if the list is big , is there a way to do it faster in python ?我不能使用 grep,我试过这个,但是如果列表很大,它需要很长时间,有没有办法在 python 中更快地做到这一点?
c = []
d = []
for i in b:
for j in a:
if i in j:
d.append(j)
c = list(set(a).difference(d))
is there a way to do it faster in python ?有没有办法在 python 中更快地做到这一点?
One way using list-comprehension
:使用
list-comprehension
一种方法:
c = [i for i in a if not any(j in b for j in i)]
print (c)
#['3', '4']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.