×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

List of tuples issue

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?

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

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

RE: List of tuples issue

(OP)
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)] 

RE: List of tuples issue

@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)] 

RE: List of tuples issue

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close