I have a problem with lists because I want to get value from the list named test_table
with value from A_table
as an argument. Is there any way to get the proper result? Of course the lists aren't empty and when I run it I get Process finished with exit code -1073740791 (0xC0000409)
for x in range(len(A_table)):
print(test_table[A_table[x]])
Edit: List_A is generated like this: (i think that the problem is type-string not integer, but with int type my function doesn't work):
A_letter = [find_all(sentence, 'A')]
A_string = ' '.join(map(str, A_letter[0]))
data = A_string.split() # split string into a list
for temp in data:
A_table.append(temp)
Is this what you are trying to do?
This code looks into test_list
and if the value is found, prints it by calling the list.index()
function.
list_a =[1,2,3,4]
test_list = [3,5,8,9]
for i in range(len(list_a)):
if list_a[i] in test_list:
print(test_list[test_list.index(list_a[i])])
//output = 3
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.
User contributions licensed under CC BY-SA 3.0