-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?

Ryan M
  • 18,333
  • 31
  • 67
  • 74
CodingBee
  • 99
  • 3
  • 7
  • 1
    What do you mean by "list item contains in string?" I'm not a native English speaker, so it might be just me. Anyway, to avoid misinterpretation, it is better to include the desired output. – j1-lee Feb 24 '22 at 03:27
  • Your problem is not well-defined. Please provide a few example inputs, and the expected output. – ddejohn Feb 24 '22 at 03: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](https://docs.python.org/3/tutorial/) of the Python language, as this is a very basic operation to perform and Stack Overflow is not a tutorial site. – ddejohn Feb 24 '22 at 03:33

4 Answers4

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.

hteza
  • 301
  • 1
  • 7
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]
Denny Chen
  • 489
  • 3
  • 8
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)
JWCompDev
  • 455
  • 1
  • 5
  • 19
0

Here is an example of what you can add. Which you can try doing a for range loop (as shown below). With the if statement and the "in" parameter, it will check if at least part of the string in the current index of the list matches with the string.

for i in range(0, len(item_list)):

    # If the current list item matches the string, then it will print
    # out what item in the list it matches with 'String_text'.
    if item_list[i] in String_text:
        print(f"'{item_list[i]}' in String_text")

Note: It does not have to be 'i', it can be any non-used variable you want it to be (ex. 'item', 'index', etc.). I just used 'i' for this example. Also note that matching the strings are case-sensitive.

I ran the code, and this was an output I got:

'Manpower Outsourcing' in String_text

Dharman
  • 30,962
  • 25
  • 85
  • 135
Paramon
  • 1
  • 1
  • 1
  • Any particular reason why you are using "i in range(0, len(item_list))" when it is simpler to use for "item in item_list"? – JWCompDev Feb 24 '22 at 04:09
  • Well if you read my answer, you would know that i was used for this example. And the range function goes through the entire list from 0 to the length of the list. While you can start from the total length of the list to 0. I prefer to start from 0. And the third value (I forgot to put it in my solution). You can put 1, so that it goes through every value in the list. – Paramon Feb 25 '22 at 16:08
  • And also I forgot to mention, the for loop you put is known as a for each loop. To me for range loop is simpler for me, became for each loops may not always work with somethings that I program. – Paramon Feb 25 '22 at 16:28
  • ok, sorry if I came across rude, that was not my intent, If you are trying to deal with index you should probably use 'for i, item in enumerate(item_list, 1)' instead of range. Range is meant to be used with numbers while enumerate is meant to be used for lists and dicts. In the end though it comes down to personal preference. Check out this link: https://dev.to/wangonya/when-to-use-python-s-enumerate-instead-of-range-in-loops-3e03 – JWCompDev Feb 25 '22 at 21:27