I have a variable length list of 2-D arrays that I'd like to optionally pass to a python function. This is a scaled back version of my first attempt.
list_of_2Darrays = [array_a,array_b,array_c]
def viol(required,*args):
dist_a = args[0]
dist_b = args[1]
dist_c = args[2]
sum = dist_a + dist_b + dist_c
output = viol(required,list_of_2Darrays)
It seemed to work for "dist_a", but when it got to the "1" position, it yielded: "IndexError: tuple index out of range". From there, I read this question here which explained that *args is read as a tuple and I can't index through a tuple.
I tried converting the tuple to a list with list(args) but then I received "IndexError: list index out of range. Next I tried implementing the approach in the question I shared above 1 but it gave me an "AttributeError: 'NoneType' object is not subscriptable." I tried to make it subscriptable using an approach I found on this other site which uses the sort function.
list_of_2Darrays = [array_a,array_b,array_c]
def foo(*args):
print(args)
def viol(required,*args):
temp = foo(args)
dists = temp.sort()
dist_a = dists[0]
dist_b = dists[1]
dist_c = dists[2]
sum = dist_a + dist_b + dist_c
output = viol(required,list_of_2Darrays)
This yielded the following error, "AttributeError: 'NoneType' object has no attribute 'sort'".
I'm sure I'm missing something simple, or just not coding this in an efficient way. If you can provide any advice, I'd greatly appreciate it.
edit: Sometimes the list of arrays is 3 elements, sometimes 5. The "required" argument essentially tells it the length of the list it's going to receive and then there's a series of if/elif/else statements that direct to the different types of calculations.