-2

Env Python3.6. Here's a list I want to match some sentence. (It's a bit tricky to replace it in English so I'll go with Japanese as it is.)

INPUT:
match_list=['…', '‥', '...', 'かも', '多分']
text = 'も'
if any(text in m for m in match_list):
    print(text)

OUTPUT: 'も'

This output seems different from my point. It actually reacts to one of elements in the list: 'かも' But what I want to know is if the text completely matches any one of the list, not partially. Is there any better way to work it out? Thanks.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
user9191983
  • 505
  • 1
  • 4
  • 20
  • 2
    `if any(text == m for m in match_list):` – Wiktor Stribiżew Jun 12 '19 at 07:11
  • 2
    Possible duplicate of [Find object in list that has attribute equal to some value (that meets any condition)](https://stackoverflow.com/questions/7125467/find-object-in-list-that-has-attribute-equal-to-some-value-that-meets-any-condi) or [How to check if one of the following items is in a list?](https://stackoverflow.com/questions/740287/how-to-check-if-one-of-the-following-items-is-in-a-list) or [Check whether an item in a list exist in another list or not python](https://stackoverflow.com/questions/20238281/check-whether-an-item-in-a-list-exist-in-another-list-or-not-python) – Wiktor Stribiżew Jun 12 '19 at 07:12
  • Can you have more than one exact matches in the list @user9191983 – Devesh Kumar Singh Jun 12 '19 at 07:16
  • @WiktorStribiżew youre very quick! thanks a lot! – user9191983 Jun 12 '19 at 11:07
  • I am glad I could help, please close the question as a duplicate, it is surely a dupe of [How to check if one of the following items is in a list?](https://stackoverflow.com/questions/740287) or [Check whether an item in a list exist in another list or not python](https://stackoverflow.com/questions/20238281) – Wiktor Stribiżew Jun 12 '19 at 11:11
  • Check if my answer below helps you @user9191983 :) – Devesh Kumar Singh Jun 12 '19 at 12:17

1 Answers1

0

Check for equality for checking if the text exactly matches an element in the list, instead of the membership operator in which checks if the text is a substring of an element in list

match_list=['…', '‥', '...', 'かも', '多分', 'も']

text = 'も'
#Match exact string via equality
if any(text == m for m in match_list):
    print(text)

The output will be

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40