Check if multiple strings exist in another string
You can use any
:
a_string = "A string is more than its parts!"
matches = ["more", "wholesome", "milk"]
if any(x in a_string for x in matches):
Similarly to check if all the strings from the list are found, use all
instead of any
.
How to check if all multiple strings exist in another string (for all row in dataframe)
this code snippet should do your work:
df = pd.DataFrame({'sub_strings': [["the sun", "rising up"], ["go home", "tomorow"]], 'string': ["the sun is rising up.", "I will go home."]})
df.head()
sub_strings string
0 [the sun, rising up] the sun is rising up.
1 [go home, tomorow] I will go home.
df['value'] = df.apply(lambda x: all([val in x.string for val in x.sub_strings]), axis=1)
df.head()
sub_strings string value
0 [the sun, rising up] the sun is rising up. True
1 [go home, tomorow] I will go home. False
How can I check if multiple strings exist in another string?
Yes, you can use grepl
(not grep
, actually), but you must run it once for each substring:
> sapply(str, grepl, myStr)
very beauti bt
TRUE TRUE TRUE
To get only one result if all of them are true, use all
:
> all(sapply(str, grepl, myStr))
[1] TRUE
Edit:
In case you have more than one string to check, say:
myStrings <- c("I am very beautiful btw", "I am not beautiful btw")
You then run the sapply
code, which will return a matrix with one row for each string in myStrings. Apply all
on each row:
> apply(sapply(str, grepl, myStrings), 1, all)
[1] TRUE FALSE
how do i check for multiple strings at once in python?
You can achieve this using regex
import re
ans = input("String to test : ")
if re.search(r"[a-zA-Z]", ans) == None: # Test for lowercase and uppercase letters
print("No letter in this string")
else:
print("Letter found")
However this will not be triggered by é è ù ì
etc.
How to check if multiple substrings appear together in a string
strings = ['I have a bird', 'I have a bag and a bird', 'I have a bag']
words = ['bird','bag']
for string in strings:
stringlist = string.split()
word1 , word2 = words
if word1 in stringlist and word2 in stringlist:
print(True)
else:
print(False)
Result
False
True
False
Check if multiple strings exists in list using Python
You have to check if all of the strings in the first list is contained by any string in the second list:
def all_exist(avalue, bvalue):
return all(any(x in y for y in bvalue) for x in avalue)
items = ['greg','krista','marie']
print(all_exist(['greg', 'krista'], items)) # -> True
print(all_exist(['gre', 'kris'], items)) # -> True
print(all_exist(['gre', 'purple'], items)) # -> False
print(all_exist([], items)) # -> True
How to check if a String contains any of some strings
If you are looking for single characters, you can use String.IndexOfAny()
.
If you want arbitrary strings, then I'm not aware of a .NET method to achieve that "directly", although a regular expression would work.
Go: how to check if a string contains multiple substrings?
You can write your own utility function using strings.Contains()
that can work for multiple sub-strings.
Here's an example that returns Boolean (true
/false
) in case of complete / partial match and the total number of matches:
package main
import (
"fmt"
"strings"
)
func checkSubstrings(str string, subs ...string) (bool, int) {
matches := 0
isCompleteMatch := true
fmt.Printf("String: \"%s\", Substrings: %s\n", str, subs)
for _, sub := range subs {
if strings.Contains(str, sub) {
matches += 1
} else {
isCompleteMatch = false
}
}
return isCompleteMatch, matches
}
func main() {
isCompleteMatch1, matches1 := checkSubstrings("Hello abc, xyz, abc", "abc", "xyz")
fmt.Printf("Test 1: { isCompleteMatch: %t, Matches: %d }\n", isCompleteMatch1, matches1)
fmt.Println()
isCompleteMatch2, matches2 := checkSubstrings("Hello abc, abc", "abc", "xyz")
fmt.Printf("Test 2: { isCompleteMatch: %t, Matches: %d }\n", isCompleteMatch2, matches2)
}
Output:
String: "Hello abc, xyz, abc", Substrings: [abc xyz]
Test 1: { isCompleteMatch: true, Matches: 2 }
String: "Hello abc, abc", Substrings: [abc xyz]
Test 2: { isCompleteMatch: false, Matches: 1 }
Here's the live example: https://play.golang.org/p/Xka0KfBrRD
How can I check if multiple words appear in a sentence?
You can check against multiple values using a combination of in
and any
. It essentially creates a list of boolean values, then checks if any of them are true (i.e. one of the words exists in the message).
if any(x in message.content for x in ["yeah", "yes"]):
# send "yep"
else:
# send "nope"
This solution is based off this answer
Related Topics
Linux Command-Line Call Not Returning What It Should from Os.System
What Does If _Name_ == "_Main_": Do
Why Is the Command Bound to a Button or Event Executed When Declared
Tkinter: Attributeerror: Nonetype Object Has No Attribute ≪Attribute Name≫
Store Large Data or a Service Connection Per Flask Session
Python Multiprocessing: Permission Denied
Django Server Killed Frequently
How to Install Pyodbc on Linux
Simulating Key Press Event Using Python For Linux
Show Matplotlib Plots (And Other Gui) in Ubuntu (Wsl1 & Wsl2)
Relative Imports For the Billionth Time
What Are Metaclasses in Python
How to Make Python Script Run as Service
Actual Meaning of 'Shell=True' in Subprocess
Post Values from an HTML Form and Access Them in a Flask View