Python Forum
Newbie question for complicated namedtuple generation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie question for complicated namedtuple generation
#1
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,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 654 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 626 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 543 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 922 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  Node Flow Generation in Python Linenloid 0 630 Feb-21-2023, 07:09 PM
Last Post: Linenloid
  numpy newbie question bcwilly_ca 4 1,128 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  Allure Report Generation rotemz 0 752 Jan-24-2023, 08:30 PM
Last Post: rotemz
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,738 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Why doesnt chunk generation work? LotosProgramer 1 1,911 Apr-02-2022, 08:25 AM
Last Post: deanhystad
  Random data generation sum to 1 by rounding juniorcoder 9 3,350 Oct-20-2021, 03:36 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020