string1 = "Billie Jean"
string2 = " "
teststring = string2.split(" ")
for word in teststring:
if word in string1:
return True
return False
Can I make it so that if string2 is for example: "Baby Jean" it's true, but if it's: "ea" it returns as false?
I believe the resolution is to split up string1 into a list of words before performing an in check, as follows:
string1 = "Billie Jean"
string2 = " "
string3 = 'Baby Jean'
words1 = string1.split()
def check(string):
teststring = string.split()
for word in teststring:
# Notice that we iterate over a list of words instead of string1
if word in words1:
return True
return False
print(check(string2))
print(check(string3))
Another option would be to use the set.intersection method, which returns true if there are any shared elements between two collections (one of which is a set):
string1 = "Billie Jean"
string2 = " "
string3 = 'Baby Jean'
words1 = set(string1.split())
def check(string):
return True if words1.intersection(string.split()) else False
print(check(string2))
print(check(string3))
Outputs:
False
True
A couple of pointers:
split() by default splits on the " " character so you don't need to explicitly pass it.
Since you are checking for word being in another string, you must split string1 too.
Check the code below:
def func(string1, string2):
for word in string2.split():
if word in string1.split():
return True
return False
print(func("Billie Jean", "Baby Jean")) # True
print(func("Billie Jean", "ea")) # False
A more interesting solution would be the following:
def func(string1, string2):
# create set of words for each string and see if intersection is empty or not
return True if set(string1.split()) & set(string2.split()) else False
print(func("Billie Jean", "Baby Jean"))
print(func("Billie Jean", "ea"))
Since matching using the intersection of sets leverages hashing, this method will be a lot faster at scale. (Might not really matter to you know, but it is good to know)
PS. You might want to convert both strings to the same case (lower or upper) if you wish to match "Jean" with "jean".
Related
I wonder how to check for sentence case in string. I know about isupper() method that checks for any uppercase character. But what about sentence case? For example:
def check_string(string):
<...>
return result
check_string('This is a proper sentence case.')
True
check_string('THIS IS UPPER CASE')
False
check_string('This string is Clean')
False
A quick trick for this would be using capitalize() and check if it is equal to the original input:
def check_string(my_string):
cap_string = my_string.capitalize()
if (cap_string == my_string):
return True
return False
def check_string(string):
sentences = string.split(".")
for sentence in sentences:
sentence = sentence.strip()
if sentence and (not sentence[0].isupper() or any(filter(lambda x: x.isupper(), sentence[1:]))):
return False
return True
Is there any library that allows me to check If all the individual characters in one string exists in another string. When i try to use in what happens is the character has to be a substring. It only works for 1234 and 123. However i want something that checks individual characters. I want a library that gives me the output: string 2 is in string 1 for the following code.
string1 = '1234'
string2 = '24'
if string2 in string1:
print('string2 is in string1')
else:
print('string2 is not in string1')
You can use all() with a generator. This returns a true only if all conditions are a true else false:
string1 = '1234'
string2 = '24'
if all(x in string1 for x in string2):
print('string2 is in string1')
else:
print('string2 is not in string1')
Or, you can use set's issubset:
set(string2).issubset(string1)
word = raw_input("Enter word: ").count('e')
def has_no_e(word):
for i in word:
if i not in word:
return True
print has_no_e(word)
Python provides a membership operator that can be used with string.
The in operator return True if the first operand is contained within the second, and False otherwise.
So
>>> "b" in "aaa"
False
and
>>> "a" in "aaa"
True
In your case you can combine it with not negation and do something like this:
def has_no_e(word):
char = 'e'
return char not in word
Using this:
if has_no_e("word-e"):
print "No e char in this word!"
else:
print "This word has e character"
Hi I am just starting to learn how to program and have a function that I need to write in Python, this is the idea behind it:
It returns True if word is in the wordList and is entirely composed of letters in the hand. Otherwise, returns False. Does not mutate hand or wordList.
There is a function to call that checks the frequency of the letters in the word the user comes up with and that is a converted to a dict, I have tried using iteritems various ways but to no avail, I am getting stuck on the words that have repeated letters, they are being returned as true when I don't have two entries for that letter in the users hand.
Sorry if this is unclear I only started two weeks ago.
any pointers would be great I have been stuck on this for a long time!
def isValidWord(hand,word,wordList):
"""
Returns True if word is in the wordList and is entirely
composed of letters in the hand. Otherwise, returns False.
Does not mutate hand or wordList.
word: string
hand: dictionary (string -> int)
wordList: list of lowercase strings
"""
wordC = getFrequencyDict(word)
handC = dict.copy(hand)
if word not in wordList:
return False
for c in word:
if c not in hand:
return False
for k,v in wordC.iteritems():
if k in hand and v > 1:
handC[k] -= 1
basically my next step was trying to figure out how to compare word to handC with amended value and discounting any key with a value of zero.
I think(hope) that will work.
Without your code, let me see if I understand what you want: you're trying to see if the given word can be spelled using the letters in hand as if the user had a Scrabble tile for each letter in hand, yes?
Personally I'd just copy the hand dictionary and then allow changes to the copy. Something like this:
def is_valid_word(hand, word, wordlist):
hand_cp = dict(hand)
for letter in word:
if hand_cp.get(letter):
# The letter is in our hand, so "use it up".
hand_cp[letter] = hand_cp[letter] - 1
else:
# The letter isn't in our hand, so the word isn't valid.
return False
# If we can make the word, now make sure it's a real word:
# (If wordlist is long, you might want to sort it and do a real search)
if word not in wordlist:
return False
# We haven't found any reason to return False, so this is a valid word.
return True
How about something like this:
def isValidWord(hand, word, word_list):
if word not in word_list:
return False
for c in word:
if c not in hand:
return False
return True
As strings are iterable you can check character by character.
Good luck
The python Counter class is your friend. You can do this in python 2.7 and later:
from collections import Counter
def is_valid_word(hand, word, word_list):
letter_leftover = Counter(hand)
letter_leftover.subtract(Counter(word))
return word in word_list and all(v >= 0 for v in letter_leftover.values())
Then:
>>> def test():
... hand = "traipse"
... word_list = ["all", "the", "words", "in", "English",
"parts", "pines", "partiers"]
... print is_valid_word(hand, "parts", word_list)
... print is_valid_word(hand, "pines", word_list)
... print is_valid_word(hand, "partiers", word_list)
...
>>> test()
True
False
False
here is mine
def isValidWord(word, hand, wordList):
"""
Returns True if word is in the wordList and is entirely
composed of letters in the hand. Otherwise, returns False.
Does not mutate hand or wordList.
word: string
hand: dictionary (string -> int)
wordList: list of lowercase strings
"""
if word in wordList:
if set(word).issubset(set(hand)):
return True
else:
return False
else:
return False
What is an elegant way to look for a string within another string in Python, but only if the substring is within whole words, not part of a word?
Perhaps an example will demonstrate what I mean:
string1 = "ADDLESHAW GODDARD"
string2 = "ADDLESHAW GODDARD LLP"
assert string_found(string1, string2) # this is True
string1 = "ADVANCE"
string2 = "ADVANCED BUSINESS EQUIPMENT LTD"
assert not string_found(string1, string2) # this should be False
How can I best write a function called string_found that will do what I need? I thought perhaps I could fudge it with something like this:
def string_found(string1, string2):
if string2.find(string1 + " "):
return True
return False
But that doesn't feel very elegant, and also wouldn't match string1 if it was at the end of string2. Maybe I need a regex? (argh regex fear)
You can use regular expressions and the word boundary special character \b (highlight by me):
Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of alphanumeric or underscore characters, so the end of a word is indicated by whitespace or a non-alphanumeric, non-underscore character. Note that \b is defined as the boundary between \w and \W, so the precise set of characters deemed to be alphanumeric depends on the values of the UNICODE and LOCALE flags. Inside a character range, \b represents the backspace character, for compatibility with Python’s string literals.
def string_found(string1, string2):
if re.search(r"\b" + re.escape(string1) + r"\b", string2):
return True
return False
Demo
If word boundaries are only whitespaces for you, you could also get away with pre- and appending whitespaces to your strings:
def string_found(string1, string2):
string1 = " " + string1.strip() + " "
string2 = " " + string2.strip() + " "
return string2.find(string1)
The simplest and most pythonic way, I believe, is to break the strings down into individual words and scan for a match:
string = "My Name Is Josh"
substring = "Name"
for word in string.split():
if substring == word:
print("Match Found")
For a bonus, here's a oneliner:
any(substring == word for word in string.split())
Here's a way to do it without a regex (as requested) assuming that you want any whitespace to serve as a word separator.
import string
def find_substring(needle, haystack):
index = haystack.find(needle)
if index == -1:
return False
if index != 0 and haystack[index-1] not in string.whitespace:
return False
L = index + len(needle)
if L < len(haystack) and haystack[L] not in string.whitespace:
return False
return True
And here's some demo code (codepad is a great idea: Thanks to Felix Kling for reminding me)
I'm building off aaronasterling's answer.
The problem with the above code is that it will return false when there are multiple occurrences of needle in haystack, with the second occurrence satisfying the search criteria but not the first.
Here's my version:
def find_substring(needle, haystack):
search_start = 0
while (search_start < len(haystack)):
index = haystack.find(needle, search_start)
if index == -1:
return False
is_prefix_whitespace = (index == 0 or haystack[index-1] in string.whitespace)
search_start = index + len(needle)
is_suffix_whitespace = (search_start == len(haystack) or haystack[search_start] in string.whitespace)
if (is_prefix_whitespace and is_suffix_whitespace):
return True
return False
One approach using the re, or regex, module that should accomplish this task is:
import re
string1 = "pizza pony"
string2 = "who knows what a pizza pony is?"
search_result = re.search(r'\b' + string1 + '\W', string2)
print(search_result.group())
Excuse me REGEX fellows, but the simpler answer is:
text = "this is the esquisidiest piece never ever writen"
word = "is"
" {0} ".format(text).lower().count(" {0} ".format(word).lower())
The trick here is to add 2 spaces surrounding the 'text' and the 'word' to be searched, so you guarantee there will be returning only counts for the whole word and you don't get troubles with endings and beginnings of the 'text' searched.
Thanks for #Chris Larson's comment, I test it and updated like below:
import re
string1 = "massage"
string2 = "muscle massage gun"
try:
re.search(r'\b' + string1 + r'\W', string2).group()
print("Found word")
except AttributeError as ae:
print("Not found")
def string_found(string1,string2):
if string2 in string1 and string2[string2.index(string1)-1]=="
" and string2[string2.index(string1)+len(string1)]==" ":return True
elif string2.index(string1)+len(string1)==len(string2) and
string2[string2.index(string1)-1]==" ":return True
else:return False