To start, I don't know where that find_all
function is defined, but if it behaves like re.findall
(which you should probably use), then it returns a list already, so by defining A_letter = [find_all(sentence, 'A')]
, you have a list of list of matches.
Consider this example:
>>> import re
>>> sentence = 'A wonderful sample of A test string'
>>> re.findall('A', sentence)
['A', 'A']
Moving on, your A_table
has a list
of str
. So there is no direct way to index into another list using the values inside of A_table
. For example, even if test_table
has the values ['A', 'B', 'C']
, the valid index values are still "0", "1", and "2", i.e. I cannot get test_table['A']
, because lists may only be indexed by int
.
If you want to get the index of a certain value (e.g. "A") in a list, you can use the index
function of list
, which returns the first index of the provided value, or raises a ValueError
if the value is not found.
For example:
>>> import re
>>>
>>> test_table=['Q','F','R','A','B','X']
>>>
>>> sentence = 'A wonderful sample of A test string'
>>> A_table = re.findall('A', sentence)
>>>
>>> for match in A_table:
... # first check to ensure the match is in the test_table
... if match in test_table:
... # ok, I know it is here, so get the index
... i = test_table.index(match)
... v = test_table[i]
... print(f'index [{i}] has value [{v}]')
...
index [3] has value [A]
index [3] has value [A]
Edit:
Here is some more info on the .index
function, and here is another link to a question that indicates your present error is related to memory corruption.