match only exact word/string in python - python

How to match exact string/word while searching a list. I have tried, but its not correct. below I have given the sample list, my code and the test results
list = ['Hi, friend', 'can you help me?']
my code
dic=dict()
for item in list:
for word in item.split():
dic.setdefault(word, list()).append(item)
print dic.get(s)
test results:
s = "can" ~ expected output: 'can you help me?' ~ output I get: 'can you help me?'
s = "you" ~ expected output: *nothing* ~ output I get: 'can you help me?'
s = "Hi," ~ expected output: 'Hi, friend' ~ output I get: 'Hi, friend'
s = "friend" ~ expected output: *nothing* ~ output I get: 'Hi, friend'
My list contains 1500 strings. Anybody can help me??

Looks like you need a map of sentences and their starting word, so you don't need to map all words in that sentence but only the first one.
from collections import defaultdict
sentences = ['Hi, friend', 'can you help me?']
start_sentence_map = defaultdict(list)
for sentence in sentences:
start = sentence.split()[0]
start_sentence_map[start].append(sentence)
for s in ["can", "you", "Hi,", "friend"]:
print s,":",start_sentence_map.get(s)
output:
can : ['can you help me?']
you : None
Hi, : ['Hi, friend']
friend : None
Also note few things from the code above
Don't use name list as name of variable because python uses it for list class
Use default dict which makes it easy to directly add entries to dictionary instead of first adding a default entry
Better descriptive names instead of mylist, or dic

In case if you just want to see if the sentence starts with a given words you can try startswith if you don;t want the searched word to be at word boundary or split()[0] if you want it to match at word boundary. As an example
>>> def foo(s): # # word boundary
return [x for x in l if x.split()[0]==s]
>>> def bar(s): # Prefix
return [x for x in l if x.startswith(s)]
Also refrain from overlaying python global name-space like what you did when you named your list as list. I have called it l in my example.

Related

Python: Locate word that contains certain string in list of tuples

I am trying to locate words that contains certain string inside a list of lists in python, for example: If I have a list of tuples like:
the_list = [
('Had denoting properly #T-jointure you occasion directly raillery'),
('. In said to of poor full be post face snug. Introduced imprudence'),
('see say #T-unpleasing devonshire acceptance son.'),
('Exeter longer #T-wisdom gay nor design age.', 'Am weather to entered norland'),
('no in showing service. Nor repeated speaking', ' shy appetite.'),
('Excited it hastily an pasture #T-it observe.', 'Snug #T-hand how dare here too.')
]
I want to find a specific string that I search for and extract a complete word that contains it, example
for sentence in the_list:
for word in sentence:
if '#T-' in word:
print(word)
import re
wordSearch = re.compile(r'word')
for x, y in the_list:
if wordSearch.match(x):
print(x)
elif wordSearch.match(y):
print(y)
You could use list of comprehension on a flattened array of yours:
from pandas.core.common import flatten
[[word for word in x.split(' ') if '#T-' in word] for x in list(flatten(the_list)) if '#T-' in x]
#[['#T-jointure'], ['#T-unpleasing'], ['#T-wisdom'], ['#T-it'], ['#T-hand']]
Relevant places: How to make a flat list out of list of lists? (specifically this answer), Double for loop list comprehension.
you would need to use re for this task
import re
a = re.search("#(.*?)[\s]",'Exeter longer #T-wisdom gay nor design age.')
a.group(0)
Note : you need to account for the Nonetype else it will throw and error
for name in the_list:
try:
if isinstance(name,(list,tuple)):
for name1 in name:
result = re.search("#(.*?)[\s]",name1)
print(result.group(0))
else:
result = re.search("#(.*?)[\s]",name)
print(result.group(0))
except:
pass

Replace a word in a String by indexing without "string replace function" -python

Is there a way to replace a word within a string without using a "string replace function," e.g., string.replace(string,word,replacement).
[out] = forecast('This snowy weather is so cold.','cold','awesome')
out => 'This snowy weather is so awesome.
Here the word cold is replaced with awesome.
This is from my MATLAB homework which I am trying to do in python. When doing this in MATLAB we were not allowed to us strrep().
In MATLAB, I can use strfind to find the index and work from there. However, I noticed that there is a big difference between lists and strings. Strings are immutable in python and will likely have to import some module to change it to a different data type so I can work with it like how I want to without using a string replace function.
just for fun :)
st = 'This snowy weather is so cold .'.split()
given_word = 'awesome'
for i, word in enumerate(st):
if word == 'cold':
st.pop(i)
st[i - 1] = given_word
break # break if we found first word
print(' '.join(st))
Here's another answer that might be closer to the solution you described using MATLAB:
st = 'This snow weather is so cold.'
given_word = 'awesome'
word_to_replace = 'cold'
n = len(word_to_replace)
index_of_word_to_replace = st.find(word_to_replace)
print st[:index_of_word_to_replace]+given_word+st[index_of_word_to_replace+n:]
You can convert your string into a list object, find the index of the word you want to replace and then replace the word.
sentence = "This snowy weather is so cold"
# Split the sentence into a list of the words
words = sentence.split(" ")
# Get the index of the word you want to replace
word_to_replace_index = words.index("cold")
# Replace the target word with the new word based on the index
words[word_to_replace_index] = "awesome"
# Generate a new sentence
new_sentence = ' '.join(words)
Using Regex and a list comprehension.
import re
def strReplace(sentence, toReplace, toReplaceWith):
return " ".join([re.sub(toReplace, toReplaceWith, i) if re.search(toReplace, i) else i for i in sentence.split()])
print(strReplace('This snowy weather is so cold.', 'cold', 'awesome'))
Output:
This snowy weather is so awesome.

How to replace all elements in list 1 with all elements from list 2 in defined function Python3

I have 2 lists and ready built pass and print of a function. I can replace each element of 1st list with each element of 2nd list separately but I am not sure how to do it all together in a function. I have searched for answers here on stackoverflow for hours now, but all the python stuff here on this subject is so old and not compatible with python 3.6. I hope you can give me a hint what to use without the use of any imported methods (e.g. if/elif or something else). Here is what I have so far:
def goodbadString(string):
for (a,b) in zip(strings, expectedResults):
string = string.replace(strings[0],expectedResults[0])
return string
strings = ['It has been a good and bad day', 'bad company',
'good is as good does!', 'Clovis is a big city.']
expectedResults = ['I am confused', 'goodbye', 'hello',
'hello and goodbye']
for string, expectedResult in zip(strings, expectedResults):
print('Sample string = ', string)
print('Expected result =', expectedResult)
print('Actual result =', goodbadString(string))
print()
This is expected result (not the whole result though)
you can see that my function did show the right result for a first "Sample string" but now I should continue for the rest of the elements (actual result for a second sample "goodbye" and so on).
I am not sure what do you want goodbadString() exactly to do. Here is an attempt:
def goodbadString(string):
idx = strings.index(string)
return string.replace(strings[idx],expectedResults[idx])
Sample string = It has been a good and bad day
Expected result = I am confused
Actual result = I am confused
Sample string = bad company
Expected result = goodbye
Actual result = goodbye
Sample string = good is as good does!
Expected result = hello
Actual result = hello
Sample string = Clovis is a big city.
Expected result = hello and goodbye
Actual result = hello and goodbye
This is actually stupid... Simply return expected string without bothering to replace anything:
def goodbadString(string):
return expectedResults[strings.index(string)]

How to match multiple words(per string) in array with a string containing multiple words

This has been resolved. See bottom of this post for a solution
I'm trying to filter out a continuous loop that has a constant feed of strings coming in(from an API).
Heres an example of the code I'm using -
I have a filter set up with an array like so:
filter_a = ['apples and oranges', 'a test', 'bananas']
a function I found on Stackoverflow like this:
def words_in_string(word_list, a_string):
return set(word_list).intersection(a_string.split())
title = 'bananas'
#(this is a continuously looping thing, so sometimes it
# might be for example 'apples and oranges')
And my if statement:
if words_in_string(filter_a, str(title.lower())):
print(title.lower())
For some reason it would detect 'bananas' but not 'apples and oranges'. It will skip right over strings with multiple words. I'm guessing it's because of the split() but I'm not sure.
Edit:
Here's another example of what I meant:
Match this and make it successful:
title = 'this is 1'
word_list = ['this is','a test']
if title in word_list:
print("successful")
else:
print("unsuccessful")
Edit 2:
Solution
title = 'this is 1'
word_list = ['this is','a test']
if any(item in title for item in word_list):
print("successful")
else:
print("unsuccessful")
I don't think your code makes sense. Let's analyze what does words_in_string do.
word_list means a list of words you want to keep, and set(word_list) transform this list into a set which only contains unique elements. In your example, transform ['apples and oranges', 'a test', 'bannanas'] into a set is {'apples and oranges', 'a test', 'bannanas'}.
Next, a_string.split() splits a_string into a list, then call set's function intersection to get the intersection of set and what a_string.split() created.
Finally, return the result.
To be more clearly, given a list of words, this function will return the words in a_string if these words are also contained in list.
For example:
given ["banana", "apple", "orange"] and a_string = "I like banana and apple". It will return {"banana", "apple"}.
But if you change list into ["bananas", "apple", "orange"], it will only return {"apple"} as banana doesn't equal to bananas.

Replace string is not changing value

I am trying to replace any i's in a string with capital I's. I have the following code:
str.replace('i ','I ')
However, it does not replace anything in the string. I am looking to include a space after the I to differentiate between any I's in words and out of words.
Thanks if you can provide help!
The exact code is:
new = old.replace('i ','I ')
new = old.replace('-i-','-I-')
new = old.replace('i ','I ')
new = old.replace('-i-','-I-')
You throw away the first new when you assign the result of the second operation over it.
Either do
new = old.replace('i ','I ')
new = new.replace('-i-','-I-')
or
new = old.replace('i ','I ').replace('-i-','-I-')
or use regex.
I think you need something like this.
>>> import re
>>> s = "i am what i am, indeed."
>>> re.sub(r'\bi\b', 'I', s)
'I am what I am, indeed.'
This only replaces bare 'i''s with I, but the 'i''s that are part of other words are left untouched.
For your example from comments, you may need something like this:
>>> s = 'i am sam\nsam I am\nThat Sam-i-am! indeed'
>>> re.sub(r'\b(-?)i(-?)\b', r'\1I\2', s)
'I am sam\nsam I am\nThat Sam-I-am! indeed'

Categories

Resources