-1

i have a list as follows:

item_list=['Manpower Service','Manpower Outsourcing','Healthcare Sanitation','Hiring 
             of Sanitation','Custom Bid For Services','Sanitation',
                 'Facility Management', 'Security Manpower Service']

and have a string like :

String_text="Manpower Outsourcing Services - Minimum Wage - Sem..."

this string changes every time. what i want is to check if any list item contains in string and i don't know how to do it? can someone please suggest me a good way?

2
  • Your problem is not well-defined. Please provide a few example inputs, and the expected output. Commented Feb 24, 2022 at 3:30
  • If you simply want to check if there are any exact matches of the substrings, then iterate over the items in item_list and use the in keyword, e.g., item in String_text. I suggest familiarizing yourself with the fundamentals of the Python language, as this is a very basic operation to perform and Stack Overflow is not a tutorial site. Commented Feb 24, 2022 at 3:33

3 Answers 3

2

Please be noted that It might as well be a NLP problem, but my solutions are not.

If you are planning to check if members of your list are in the string, it should be pretty straight forward.

[i for i in item_list if i in String_text]
... ['Manpower Outsourcing']

This will keep only the list members that were in the string, but note it will only keep "exact matches".

If this output is not suitable for your purpose, there might be several other ways you can check.

Mark as 1 for members that were in the string, but 0 for others.

[1 if i in String_text else 0 for i in item_list]
... [0, 1, 0, 0, 0, 0, 0, 0]

Or if you would like to check how much for each members were in the string, I recommend splitting them.

item_list2 = [i.split(" ") for i in item_list]
[sum([1 if i in String_text else 0 for i in x])/len(x) for x in item_list2]
... [1.0, 1.0, 0.0, 0.0, 0.25, 0.0, 0.0, 0.6666666666666666]

You will notice the last one have different output from the formers because the first member "Manpower Service" is present seperately in the string as "Manpower" and "Service". You can choose the suitable solution for your purpose.

Again, please be noted that this might be a NLP problem and my solutions are just dumb strings matching.

Sign up to request clarification or add additional context in comments.

Comments

1

I am quite confused about "this string changes every time", but I hope the code below can solve your question.

[x for x in item_list if x in String_text]

Comments

0

The easiest way to do this is to loop through the values in item_list and use the in keyword to check if each item is in the String_text string:

found = False
found_item = ""

for item in item_list:
    found = item in String_text

    if found:
        found_item = item
        break

print("Was item found: " + str(found))

if found:
    print("Item Found: " + found_item)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.