1

Original problem was mis-stated. Three columns involved in the problem: first is an array of values (1,2,3,...) second is an array that results from multiplying array1 by a constant. The third is an array that results from operating a function such as max(current value of array2, prior value of array3)such that the first entry of array3 is null/non-existent. Thus array3 uses prior evaluation of itself...that's why the first entry of array3 is non-existent.

I've not succeeded in defining the variable for the third array. Errors such as "unresolved reference" when referring to same variable in the definition of itself; I found workaround to avoid "float is not subscriptable" errors for multiplying float by array...but so far have been unable to complete this task of resolving current and prior values of arrays in a variable. Help please!

Per the last ask:

constant1 = 2

array1 = [1, 2, 3, 4, 2...]

array2 = [i * constant1 for i in array1]

array3 = max(array2[0], array3[-1]) -----> please note correction of the problem statement...prior value of array3 is needed to evaluate current value of array3

but array3 generates an error I cannot resolve. Not subscritable...

mbmt
  • 53
  • 6
  • in python array2[-1] shouldn't produce an error, it is the last member of the array. -ve means count from backwards. The problem is array3 isn't array, you are doing max in 2 values so it is just one value not array. – Atreyagaurav Dec 16 '19 at 04:29
  • I mis-stated the problem. Apologies. array3 needs to evaluate current value of array2 and prior value of array3. I have represented "current value" with the subscript [0] while I've used [-1] to represent "prior value" in my problem description. – mbmt Dec 21 '19 at 03:31

1 Answers1

0

You have to make a new array and calculate every member like this. Or use package like pandas or numpy to do the calculations.

constant1 = 2

array1 = [1, 2, 3, 4, 2]

array2 = [i * constant1 for i in array1]

array3 = [None,array2[0]]
for i in range(2,len(array1)):
    array3.append(max(array2[i], array3[i-1]))

print(array3)
#Prints [None, 4, 6, 8, 8]

your code array3=max(array1[0], array2[-1]) outputs a single value, max of first element of array1 and last of array2, so it's not subscriptable, print that value and you'll see.

Atreyagaurav
  • 1,145
  • 6
  • 15
  • Look at [this post](https://stackoverflow.com/questions/20095673/shift-column-in-pandas-dataframe-up-by-one), you can create a lag column and then find the difference, NA values are shown as NaN, and there are other operations. – Atreyagaurav Dec 17 '19 at 05:18
  • I mis-stated the problem. Please see above correction. – mbmt Dec 21 '19 at 03:34
  • I have edited the answer to make it work, but I don't think your problem statement makes sense, because if you compare max with previous value of array3, and the first memeber of array3 is N/A, all your values will be NA, coz `max(someValue,NA)=NA`. – Atreyagaurav Dec 21 '19 at 06:10
  • Thanks - to get around the initial NA, using a value of zero is fine and your code works for a defined set of arrays where len resolves but for streaming data series, I'm getting index out of range errors. Any thoughts how to resolve? Are there any alternative solutions using pandas or numpy that don't use len? – mbmt Dec 22 '19 at 03:32
  • what do you mean streaming set of data? Please define your problem properly. If you have array1= something then it is fixed. I can't help you without knowing how do you get the stream of data. I can guess that using while loop might fix your problem, just calculate every member of all arrays in that loop. – Atreyagaurav Dec 22 '19 at 04:06
  • I'm a beginner so maybe I've got the diagnosis wrong. While running debug, I see that the line "for i in range(2, len(array1)):" is where the index out of range exception is being generated. Perhaps I'm not describing array1 correctly. This might be a case of "I need to learn the basics more before being able to describe the problem properly". – mbmt Dec 22 '19 at 04:56
  • can you add more specific example? like if you have 6 members in a array and range(2,6) shouldn't produce any error. – Atreyagaurav Dec 22 '19 at 06:09
  • your solution works fine with many members in solution as above...range(2,100)..no problem..but the problem description is only a bolt-on to a larger code base on which I am trying to add the array3 calcs. So when using the actual data source instead of array1, that's when the index out of range exception is thrown. Actual data source is a LineBuffer object within which there is a target array which I'm calling array1 in my problem description.. – mbmt Dec 22 '19 at 15:28
  • ...the key problem appears to be that range and len operations on the actual target array are not able to see-into the linebuffer object ("unable to display children:Error resolving variables..." on both range and len).. the array1 I've described is actually array1 = self.datas which is the linebuffer object ...maybe there's an alternative way to do this using pandas? – mbmt Dec 22 '19 at 15:45
  • Again, it'd be far better to show your code in both cases. If what you are using is same as [this](https://docs.python.org/2.0/api/bufferObjects.html) or [this](https://stackoverflow.com/questions/9612037/python-get-size-of-buffer). There must be a way to find the length. or just use a while loop to break when it no loneger has any members. – Atreyagaurav Dec 23 '19 at 03:05
  • while debugging in the larger code base, array2 remains a list with only one member and array3 a list with only 2 members even though len(array1) evaluates to 31 so that's why the "index out of range" exception is being thrown..which points to a solution that uses a while loop that you suggested earlier...can you show your solution with a while loop? I'm not sure how to go about it.. also appreciate if you can try your solution with pandas? Thanks again – mbmt Dec 25 '19 at 16:25
  • array2 remains a list with only one member because array1 is an iterator that updates the values in array1 every time it is called...looks like every time i increments in the calculation of array2 and array3, array1 gets updated so i restarts. I cannot control array1 because it is a datasource from an external program that takes the form self.dataseries.targetseries...perhaps there's a way to fix the array1 values just while array2 and array3 are calculated in that loop? – mbmt Dec 26 '19 at 14:18