101 posts now
What should I mark as the solution?
I should mark one and ask even similar questions in other threads?
So many solutions, highlighting comments:
5, 16, 14, 40, 49, 67 , 71
+/- 1 as I don’t really know how comments are counted.
I don’t understand comment 57 by Jordan, the dictionary is complex and I did not follow how it worked.
Even with the post linked to using the nested dictionaries, I’m still not sure.
You’re very close.
Based on what you have, I’m assuming this is a PyDataSet. Let me know if I’m off.
x = range(71)
# Mock up some values
headers = ['col{}'.format(idx) for idx in x]
data = [[100 + idx for idx in x]]
# Make a dataset and a PyDataSet to play with
datasetIn = system.dataset.toDataSet(headers, data)
currentVal = system.dataset.toPyDataSet(datasetIn)
# You figured how to get every fifth starting with 1
grossIndexes=x[1::5]
# You can do the same thing with currentVal
grossValues = currentVal[0][1::5]
#Or, to sum it all up in one go
summedVal = sum(currentVal[0][1::5])
print grossValues
print summedVal
Just so I’m on the same page, your “first” post intimated data like [[0,1,2,3,4,5,6,1,1]]. If it’s really [[1,2],[3,4],[5,6],[1,1]], then we’ll need to switch it up a bit.
Oh, wait. You’re still at 8.1.0, right? No upgrade yet? There were later changes to make pyDataSets more friendly. I think you may have to coerce it to a list. Been a while since I’ve had to think about it. lol
Try something like: print list(currentVal[0])
If it works, then put the slice after it. value=sum(list(currentVal[0])[1::5])
why does value=sum(list(currentVal[0])[1::5])
work?
I don't understand that at all. currentVal[0] isn't anything is it?
I also don't know how to test print from a script I am running that isn't in the script console.
The datatype of the row is DatasetUtilities.PyDataSet.PyRow, which (IIRC) you could only reference from a single integer index or the column name. A slice, like [1::5], was not allowed. EDIT: It’s also why your ‘ugly’ code worked, because you were referencing individual elements.
Turning the row into a list lets us be a bit more flexible.
If you’re in the designer, using print, or systyem.perspective.print() if you’re designing in Perspective will print to the designer console (Tools → Console).
The other spot I got stuck a bit was in expression language.
Is it possible to benefit from list comprehensions in expression language?
I had setup an expression tag. I don’t think there is a script tag, except working from one tag, to write to a memory tag.
You can call custom functions in a list comprehension so maybe like
def divideHandleZero(a, b):
try:
return a/b
except ZeroDivisionError:
return 'some value you want when you divide by zero'
net= [1, 2, 3, 0, 4]
eff =[.5, 0.75, 0, 1, 3]
totals=[divideHandleZero(a, b) for a,b in zip(net,eff)]
Yea I was going to say say that should have worked.
That list comprehension is about the limit for me in terms of putting logic into a single line, if you need anything more complicated I’d recommend making a custom function and calling it inside your list comprehension. Especially if you think there may be other usages of it elsewhere in your program.
if an else statement is needed, you’ll need to change up the order.
totals = [a/b if b != 0 else 0 for a,b in zip(net,eff)]
The difference is in the operation. An ‘if’ at the end checks after the calculation is done.
EDIT2: Not completely sure on the overall operation, as removing the else at the end of the line gives all values where b != 0
EDIT: This might get to the edge of readability, though.