Python Forum
Detecting String Elements in a List within Given Message - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Detecting String Elements in a List within Given Message (/thread-16876.html)



Detecting String Elements in a List within Given Message - Redicebergz - Mar-18-2019

Hello all,
So I'm trying to take a message and compare it to elements in a list, here is example code

message = 'foo bar hello'

example_list = {'foo bar', 'foo'}

if message beginswith strings_In_example_list_index
	print(the_index_in_the_list)
The last two lines are just pseudocode, because I don't know how to finish it. Given a list of any number of elements, in this case just two, if message begins with a string that is in the list, I just want it to print that element that was found to begin in the message.

Example wanted inputs to outputs:
message = 'foo hello' ------> output: foo
message = 'foo bar testing' ------> output: foo bar
message = foo foo bar ------> output: foo

I may not have explained this super well, but any help would be greatly appreciated on how to achieve these outputs.


RE: Detecting String Elements in a List within Given Message - Yoriz - Mar-18-2019

Note that example_list is actually a set because its using {} not []
message = 'foo bar hello'
 
example_list = {'foo bar', 'foo'}

 
for item in example_list:
    if message.startswith(item):
        print(item)
Output:
foo foo bar



RE: Detecting String Elements in a List within Given Message - ichabod801 - Mar-18-2019

Loop over the words in example_list, and test each one with message.startswith(word).


RE: Detecting String Elements in a List within Given Message - Redicebergz - Mar-19-2019

(Mar-18-2019, 08:13 PM)ichabod801 Wrote: Loop over the words in example_list, and test each one with message.startswith(word).

The problem that I'm still having with this is if I have two elements such as 'foo' and 'foo bar' that it will detect both of them if message is 'foo bar' because message still starts with 'foo'. I would just want it to detect and print 'foo bar'


RE: Detecting String Elements in a List within Given Message - buran - Mar-19-2019

def check(msg, strings):
    for my_str in sorted(list(strings), reverse=True):
        if msg.startswith(my_str):
            return my_str
            
example_list = {'foo bar', 'foo'}
messages = ['foo hello', 'foo bar testing', 'foo foo bar']
for msg in messages:
    print(check(msg, example_list))



RE: Detecting String Elements in a List within Given Message - Redicebergz - Mar-19-2019

(Mar-19-2019, 02:34 PM)buran Wrote:
def check(msg, strings): for my_str in sorted(list(strings), reverse=True): if msg.startswith(my_str): return my_str example_list = {'foo bar', 'foo'} messages = ['foo hello', 'foo bar testing', 'foo foo bar'] for msg in messages: print(check(msg, example_list))

Ah, that's what I was looking for. Thanks :)


RE: Detecting String Elements in a List within Given Message - buran - Mar-19-2019

yeah, and actually you don't even need to convert to list, i.e.
for my_str in sorted(strings, reverse=True):