i have two list , a = ["1","3","4","22","2"]
and b = ["1","2"]
is there a way to get the output c = ["3","4"]
in python, just like
grep -vf b a
in bash if you have file a
1
3
4
22
2
and 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 ?
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 ?
One way using list-comprehension
:
c = [i for i in a if not any(j in b for j in i)]
print (c)
#['3', '4']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.