Finding a strings that contain different substrings

MadmanLee Source

The data i have is equivalent to this,

fun_strings=[]
fun_strings=['abc','cat','but','cab','mug','xyz']

I need to collect strings from this list that contain a

'u' or 't'

my desired output is,

['cat','but','mug']

It seems i can do this the slow way with a list

[x for x in fun_strings if 'u' in x or 't' in x]

Can i do this a faster way with numpy?

pythonnumpy

Answers

answered 5 months ago JahKnows #1

You should use a or in this case

fun_strings=['abc','cat','but','cab','mug','xyz']
[x for x in fun_strings if 'u' in x or 't' in x]

['cat', 'but', 'mug']

answered 5 months ago Ayodhyankit Paul #2

Just filter the result :

print(list(filter(lambda x:'u' in x or 't' in x,fun_strings1)))

output:

['cat', 'but', 'mug']

comments powered by Disqus