Python Forum
Detecting String Elements in a List within Given Message
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detecting String Elements in a List within Given Message
#1
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.
Reply
#2
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
Reply
#3
Loop over the words in example_list, and test each one with message.startswith(word).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(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'
Reply
#5
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))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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 :)
Reply
#7
yeah, and actually you don't even need to convert to list, i.e.
for my_str in sorted(strings, reverse=True):
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 428 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 470 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Checking if a string contains all or any elements of a list k1llcod3 1 1,094 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  iterating and detecting the last Skaperen 3 1,067 Oct-01-2022, 05:23 AM
Last Post: Gribouillis
  How to change the datatype of list elements? mHosseinDS86 9 1,955 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  Detecting float or int in a string Clunk_Head 15 4,478 May-26-2022, 11:39 PM
Last Post: Pedroski55
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,115 May-17-2022, 11:38 AM
Last Post: Larz60+
  module detecting if imported vs not Skaperen 1 1,669 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  detecting a generstor passed to a funtion Skaperen 9 3,570 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  Why am I getting list elements < 0 ? Mark17 8 3,117 Aug-26-2021, 09:31 AM
Last Post: naughtyCat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020