string sieving with python -


assume string. example:

s = "fishcatraincomp_catwater_jamdog" 

and define sieve. sieve - list of words catch up(once if multiple occurrence in s), example:

sieve = ["dog","cat"] 

passing string through sieve should produce string, in our case be:

out = "catdog" 

what elegant way achieve result?

here elegant way comes mind:

''.join([word word in sieve if word in s]) 

given order of words in input string should reflected in output string:

def sievestring(s,sieve):     zipped = zip(sieve,[s.index(word) word in sieve if word in s])     zipped.sort(key=lambda x:x[1])     return ''.join(word word,index in zipped) 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -