Home > Net >  Find minimum of tuple
Find minimum of tuple

Time:10-30

I'm unable to get the minimum (the float in the example) of a list of tuples. I converted them to an array and can't get the minimum there either. I also want to sort the order. I can't figure out how to do this.

[(' handicapped-infants', 0.4255567528735632),
 (' water-project-cost-sharing', 0.49741024814695456),
 (' adoption-of-the-budget-resolution', 0.25584421930900736),
 (' physician-fee-freeze', 0.05752597041257758),
 (' el-salvador-aid', 0.21445519728116716),
 (' religious-groups-in-schools', 0.3994529378797299),
 (' anti-satellite-test-ban', 0.37440262843488636)]

CodePudding user response:

list_of_tuples.sort(key=lambda t: t[1])

CodePudding user response:

I tried both answers and it still wouldn't work with a tuple. I got "Tuple object has no attribute sort". However, I converted it to a list using the code you suggested and it works. I was able to get the list sorted and find the minimum pair. Here's the code I used.

def impurities(df):
    a = []
    for f in features[1:]:
        a.append([f, impurity(df,f)])
        a.sort(key=lambda t: t[1])
    return a #(or a[0] for minimum)
impurities(df)
  • Related