List of tuples issue
List of tuples issue
(OP)
Greetings,
I have a series of lab label/factor pairs in a list of tuples. I need to work through the factorPotency list retaining the label/factor pairs with the highest factor value in the event of duplicate label names e.g. 'chain'.
Could someone assist with this query?
I have a series of lab label/factor pairs in a list of tuples. I need to work through the factorPotency list retaining the label/factor pairs with the highest factor value in the event of duplicate label names e.g. 'chain'.
Could someone assist with this query?
CODE
factorPotency = [('chain', 100), ('compound', 100), ('sequence', 95), ('crystcont', 80), ('sequence', 75), ('chain', 67)]
CODE
Desired output: effectiveFactorPotency = [('chain', 100), ('compound', 100), ('sequence', 95), ('crystcont', 80)]
RE: List of tuples issue
Create a dictionary
Iterate through factorPotency
If the value exists in the dictionary, take the greater value
if it does not exist, add it to the dictionary
Convert dictionary back into a list
RE: List of tuples issue
I got the required result via the below routine. I am intrigued by the suggested dictionary approach. If someone could attempt the dictionary method I would be grateful to further my learning.
@xwb thanks for your response.
CODE
RE: List of tuples issue
You wrote, that you need pairs with the highest factor value, but if you change your input list to
CODE
CODE
RE: List of tuples issue
The 'factorPotency' list has already been pre-sorted by a routine by the second data-pair item (the numeric). The final outstanding issue that required addressing was the removal of 'lower' numeric data-pairs for a given label.
I would certainly be interested in an alternate approach to this task, perhaps as suggested with a dictionary, to advance my understanding. Thank you for responding.