Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List of tuples issue

Status
Not open for further replies.

marm1

Technical User
Nov 12, 2020
9
GB
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?
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)]
 
The algorithm is roughly this: leave you to do the coding

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
 
Hello,

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:
factorPotency = [('chain', 100), ('compound', 100), ('sequence', 95), ('crystcont', 80), ('sequence', 75), ('chain', 67)]
MAX = len(factorPotency)
uniqueLabel = []
effectiveFactorPotency = []
print(factorPotency)

for i in range(MAX):
    factorPotencyList = list(factorPotency[i])
    label = factorPotencyList[0]
    factor = factorPotencyList[1]

    if label not in uniqueLabel:
        uniqueLabel.append(label)
        effectiveFactorPotency.append( (label,factor) ) 

print(effectiveFactorPotency)


$ python labpot.py
[('chain', 100), ('compound', 100), ('sequence', 95), ('crystcont', 80), ('sequence', 75), ('chain', 67)]
[('chain', 100), ('compound', 100), ('sequence', 95), ('crystcont', 80)]
 
@marm1,
You wrote, that you need pairs with the highest factor value, but if you change your input list to
Code:
factorPotency = [('chain', 67), ('compound', 100), ('sequence', 75), ('crystcont', 80), ('sequence', 95), ('chain', 100)]
you will not get the pairs with highest values, but the pairs which appear first in the list, i.e. the result would be this:
Code:
[('chain', 67), ('compound', 100), ('sequence', 75), ('crystcont', 80)]
 
Thank you @mikrom. You are quite right. I should have provided more background and context to this task. This task is the final part of a larger piece of work.

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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top