JavaScript
x
6
1
matrixA =
2
[['AAA', 'BBB', 'CCC'],
3
['PPP', 'QQQ', 'RRR', 'SSS'],
4
['DDD','EEE','FFF'],
5
['GGG', 'HHH', 'III']]
6
how to remove the above elements from listA from matrixA
JavaScript
1
2
1
listA = ['DDD', 'EEE', 'FFF']
2
Desired output:
JavaScript
1
4
1
output_matrix =
2
[['DDD','EEE','FFF'],
3
['GGG', 'HHH', 'III']]
4
sorry for my bad language
Advertisement
Answer
You can get the index
of listA
within matrixA
and then slice with it:
JavaScript
1
3
1
idx = matrixA.index(listA)
2
out = matrixA[idx:]
3
to get
JavaScript
1
4
1
>>> out
2
3
[["DDD", "EEE", "FFF"], ["GGG", "HHH", "III"]]
4
in case listA
doesn’t exist, it gives an error so we try
:
JavaScript
1
9
1
try:
2
idx = matrixA.index(listA)
3
except ValueError:
4
# if it doesn't exist..
5
out = matrixA # we can take the whole `matrixA`, for example
6
else:
7
# it does exist
8
out = matrixA[idx:]
9