7

very new to this so bear with me please...

I got a predefined list of words

checklist = ['A','FOO']

and a words list from line.split() that looks something like this

words = ['fAr', 'near', 'A']

I need the exact match of checklist in words, so I only find 'A':

if checklist[0] in words:

That didn't work, so I tried some suggestions I found here:

if re.search(r'\b'checklist[0]'\b', line): 

To no avail, cause I apparently can't look for list objects like that... Any help on this?

Amir
  • 10,600
  • 9
  • 48
  • 75
origamisven
  • 83
  • 1
  • 1
  • 4

4 Answers4

18

Using a set would be much faster than iterating through the lists.

checklist = ['A', 'FOO']
words = ['fAr', 'near', 'A']
matches = set(checklist).intersection(set(words))
print(matches)  # {'A'}
Michelle Welcks
  • 3,513
  • 4
  • 21
  • 34
6

This will get you a list of exact matches.

matches = [c for c in checklist if c in words]

Which is the same as:

matches = []
for c in checklist:
  if c in words:
    matches.append(c)
pushkin
  • 9,575
  • 15
  • 51
  • 95
  • 2
    This will not return exact matches, it will also return partial matches. – eDonkey Jul 13 '22 at 13:07
  • This answer is correct for returning partial matches not exact matches – A.M Oct 06 '22 at 14:36
  • @A.M what do you mean "partial matches"? – pushkin Oct 06 '22 at 21:05
  • @pushkin, Partial matches means if you have a list `a = ['FOO', 'FOOL', 'A', 'B']` and looking for only string FOO in the list, your code appends both FOO and FOOL to the matches list, which means your code append both exact match ('FOO') and partial match ('FOOL') and the question is 'Find exact match in list of strings' :) – A.M Oct 07 '22 at 08:23
  • @A.M I don't believe that's the case. I'm doing string `in` a list. That will return exact matches (see [here](https://www.askpython.com/python/examples/in-and-not-in-operators-in-python)). `in` will do a substring match if `words` in my code were itself a string. I just ran this code and it returned only exact matches – pushkin Oct 07 '22 at 14:49
  • @JimArcher What are you basing that off of? I just tried running code like `a = ["ab"];print("a" in a)`, and it returned `False` – pushkin Aug 15 '23 at 13:48
  • Yes that does work, I was seeing opposite behavior the other day. When I have time I'll try to reproduce it. I'll take the comment down for now. – Jim Archer Aug 15 '23 at 18:55
  • I also think this returns partial matches as I'm trying to find a solution to get exact matches, and the existing code that I have uses `in` and it returns partial matches. – rksh Aug 18 '23 at 12:31
  • @rksh are you able to post your code? In all my tests and prior experience, `in` only does partial matches when you're using it on a string. On array elements, it returns complete matches for me. – pushkin Aug 18 '23 at 13:57
1

Set will meet your needs. There is an issubset method of set. The example is like following:

checklist = ['A','FOO']
words = ['fAr', 'near', 'A']

print set(checklist).issubset(set(words))

If you only need test if there is comment element in two list, you could change to intersection method.

Xiaoqi Chu
  • 1,497
  • 11
  • 6
0

Let me know if this works for you,

In [67]: test = re.match(r"(.*?)A(.*?)$", "CAT")

In [68]: test.group(2)

Out[68]: 'T'

In [69]: test.group()

Out[69]: 'CAT'

In [70]: test.group(1)

Out[70]: 'C'

If the pattern in does not match, the test object does not exists.

NHMPlus
  • 1
  • 1