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


Messages In This Thread
Detecting String Elements in a List within Given Message - by Redicebergz - Mar-18-2019, 07:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 609 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 591 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Checking if a string contains all or any elements of a list k1llcod3 1 1,273 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  iterating and detecting the last Skaperen 3 1,184 Oct-01-2022, 05:23 AM
Last Post: Gribouillis
  How to change the datatype of list elements? mHosseinDS86 9 2,165 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  Detecting float or int in a string Clunk_Head 15 5,054 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,458 May-17-2022, 11:38 AM
Last Post: Larz60+
  module detecting if imported vs not Skaperen 1 1,751 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  detecting a generstor passed to a funtion Skaperen 9 3,842 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  Why am I getting list elements < 0 ? Mark17 8 3,315 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