Check If a String Contains an Element from a List (Of Strings)

How to check if a string contains an element from a list in Python

Use a generator together with any, which short-circuits on the first True:

if any(ext in url_string for ext in extensionsToCheck):
print(url_string)

EDIT: I see this answer has been accepted by OP. Though my solution may be "good enough" solution to his particular problem, and is a good general way to check if any strings in a list are found in another string, keep in mind that this is all that this solution does. It does not care WHERE the string is found e.g. in the ending of the string. If this is important, as is often the case with urls, you should look to the answer of @Wladimir Palant, or you risk getting false positives.

Check if list contains element that contains a string and get that element

You should be able to use Linq here:

var matchingvalues = myList
.Where(stringToCheck => stringToCheck.Contains(myString));

If you simply wish to return the first matching item:

var match = myList
.FirstOrDefault(stringToCheck => stringToCheck.Contains(myString));

if(match != null)
//Do stuff

How to check if a string is a substring of items in a list of strings

To check for the presence of 'abc' in any string in the list:

xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456']

if any("abc" in s for s in xs):
...

To get all the items containing 'abc':

matching = [s for s in xs if "abc" in s]

How to check for the presence of a substring in a list of strings

You have a numpy array, not a list.

Anyway, considering a list (this would also work on a numpy array):

my_lst = ['hello', 'this', 'is', 'a', 'testello']

query = 'ello'
out = [query in e for e in my_lst]

# [True, False, False, False, True]

for a numpy array:

my_array = np.array(['hello', 'this', 'is', 'a', 'testello'])

out = np.core.defchararray.find(my_array, query)>0
# array([ True, False, False, False, True])

Dart check if part of a string in a a list of strings contains an element

Check whether any element of the iterable satisfies test

test(String value) => value.contains(s);

listS.any(test);

Check if a string contains an element from a list (of strings)

With LINQ, and using C# (I don't know VB much these days):

bool b = listOfStrings.Any(s=>myString.Contains(s));

or (shorter and more efficient, but arguably less clear):

bool b = listOfStrings.Any(myString.Contains);

If you were testing equality, it would be worth looking at HashSet etc, but this won't help with partial matches unless you split it into fragments and add an order of complexity.


update: if you really mean "StartsWith", then you could sort the list and place it into an array ; then use Array.BinarySearch to find each item - check by lookup to see if it is a full or partial match.

Detect if string contains any element of a string array

In additional to answer of @Sh_Khan, if you want match some word from group:

let str:String = "house near the beach"
let wordGroups:[String] = ["beach","waterfront","with a water view","near ocean","close to water"]
let worlds = wordGroups.flatMap { $0.components(separatedBy: " ")}
let match = worlds.filter { str.range(of:$0) != nil }.count != 0

How to check if a list (string) contains another list (string) considering order

I believe that this answer should work if you just don't remove things from the sublist that aren't in the test list. So for the case of the first method provided there

def contains(testList, subList):
shared = [x for x in testList if x in subList]
return shared == subList

You could also convert the sublist to work with non-list inputs.

def contains(testList, subList):
shared = [x for x in testList if x in subList]
return shared == list(subList)

How to find if a string contains any items of an List of strings?

The simplest code I could come up with would be:

var hasAny = children.Any(motherString.Contains);

If you expect each of the words to be seperated by a space then you could use this:

var hasAny = motherString.Split(new[] { ' ' }).Any(children.Contains);

If the words in motherString could be seperated by other characters, you could add them like this:

motherString.Split(new[] { ' ', ',', ':' })


Related Topics



Leave a reply



Submit