Python Forum

Full Version: Newbie question for complicated namedtuple generation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello:
I have some code by hand:
import collections
listA = [ 1.1, 1.2, 1.3, 1.4, 1.2, 1.1, 1.0, 1.1, 1.2, 1.3 ]
listB = [-1.0,-1.1,-1.2,-1.3,-1.1,-1.0,-0.9,-1.0,-1.1,-1.2 ]
listC = [ 0.0, 1.0, 1.2, 1.3, 0.0, -1.0, -1.2, 1.2, 0.0, -1.0]

threshold1 = 1.0
'''
KeyValue = collections.namedtuple('KeyValue', ['Key', 'Value'])
lTuples = []
lTuples.append(KeyValue(Key = 0, Value = 0.0))
lTuples.append(KeyValue(Key = 1, Value = 1.2))
lTuples.append(KeyValue(Key = 0, Value = 0.0))
lTuples.append(KeyValue(Key = 0, Value = 0.0))
lTuples.append(KeyValue(Key = 0, Value = 0.0))
lTuples.append(KeyValue(Key =-2, Value =-1.0))
lTuples.append(KeyValue(Key = 0, Value = 0.0))
lTuples.append(KeyValue(Key = 2, Value = 1.1))
lTuples.append(KeyValue(Key = 0, Value = 0.0))
lTuples.append(KeyValue(Key =-1, Value =-1.2))
'''
I have 3 lists: listA, listB, listC and a value of threshold1.
listA, listB, listC have the same length, here it is 10.
All the values in listA are positive, all the values in listB are negative.
I want to make a list of namedtuple with the following rules:
For the given value of threshold1, loop through each item in listC, if the absolute value of item in listC is more than threshold1, then if the item in listC is positive value, then pick up the 'Value' for namedtuple from listA, for the first/last item meets the requirement, the 'Key' value for namedtuple is 1, the sign of 'Key' is the sign of item in listC; for the items within first/last range, the 'Key' value for namedtuple is 2, the sign of 'Key' is the sign of item in listC.
If the absolute value of item in listC is more than threshold1, then if the item in listC is negative value, then pick up the 'Value' for namedtuple from listB, for the first/last item meets the requirement, the 'Key' value for namedtuple is 1, the sign of 'Key' is the sign of item in listC; for the items within first/last range, the 'Key' value for namedtuple is 2, the sign of 'Key' is the sign of item in listC.
If the absolute value of item in listC is less than threshold1, or if the previous non-zero namedtuple 'Key' is the same as the current item in listC, then the underlying lTuples is (KeyValue(Key = 0, Value = 0.0))

Another example using the above listA, listB, listC, but if threshold1 = 1.4; then all namedtuple in lTuples are (KeyValue(Key = 0, Value = 0.0))

Another example using the above listA, listB, listC, but if threshold1 = 1.2; then I want to have the following results:
import collections

listA = [ 1.1, 1.2, 1.3, 1.4, 1.2, 1.1, 1.0, 1.1, 1.2, 1.3 ]
listB = [-1.0,-1.1,-1.2,-1.3,-1.1,-1.0,-0.9,-1.0,-1.1,-1.2 ]
listC = [ 0.0, 1.0, 1.2, 1.3, 0.0, -1.0, -1.2, 1.2, 0.0, -1.0]
threshold1 = 1.2
KeyValue = collections.namedtuple('KeyValue', ['Key', 'Value'])
lTuples = []
lTuples.append(KeyValue(Key = 0, Value = 0.0))
lTuples.append(KeyValue(Key = 0, Value = 0.0))
lTuples.append(KeyValue(Key = 1, Value = 1.3))
lTuples.append(KeyValue(Key = 0, Value = 0.0))
lTuples.append(KeyValue(Key = 0, Value = 0.0))
lTuples.append(KeyValue(Key = 0, Value = 0.0))
lTuples.append(KeyValue(Key =-2, Value =-0.9))
lTuples.append(KeyValue(Key = 1, Value = 1.1))
lTuples.append(KeyValue(Key = 0, Value = 0.0))
lTuples.append(KeyValue(Key = 0, Value = 0.0))
Note: the sum of 'Key' value in namedtuple is always 0, for the above example, the sum of 'Key' in namedtuple is: 1 + (-2) + 1 =0
I want to have a function to do this, but with lambda or other array/list built-in functions.
Thanks,