Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4068548
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:23:19+00:00 2026-05-20T16:23:19+00:00

For example I have a non-ordered list of values [10, 20, 50, 200, 100,

  • 0

For example I have a non-ordered list of values [10, 20, 50, 200, 100, 300, 250, 150]

I have this code which returns the next greater value:

def GetNextHighTemp(self,  temp,  templist):
    target = int(temp)
    list = []
    for t in templist:
        if t != "":
            list.append(int(t))
    return str(min((abs(target - i), i) for i in list)[1])

e.g. If temp = 55, it will return ‘100’.

But how can I get the lesser of the value? That is how to get it to return ’50’?

Thank you.

EDIT – now working

def OnTWMatCurrentIndexChanged(self):
    self.ClearTWSelectInputs()
    material = self.cb_TW_mat.currentText()
    temp = self.txt_design_temp.text()
    if material != "":
        Eref = self.GetMaterialData(material,  "25",  "elast")
        if Eref and Eref != "":
            Eref = str(float(Eref) / 1000000000)
            self.txt_TW_Eref.setText(Eref)
        else:
            self.txt_TW_Eref.setText("194.8")
            self.ShowMsg("No temperature match found for E<sub>ref</sub> in material data file. Value of 194.8 GPa will be used.",  "blue")
    if material != "" and temp != "":
        if self.CheckTWTemp(material,  temp):
            dens = self.GetMaterialData(material,  temp,  "dens")
            self.txt_TW_dens.setText(dens)
            elast = self.GetMaterialData(material,  temp,  "elast")
            elast = str(float(elast) / 1000000000)
            self.txt_TW_Et.setText(elast)
            stress = self.GetMaterialData(material,  temp,  "stress")
            stress = str(float(stress) / 1000000)
            self.txt_TW_stress_limit.setText(stress)
        else:
            self.ShowMsg("No temperature match found for " + temp + "&#x00B0; C in material data file. Extrapolated data will be used where possible or add new material data.",  "blue")
            dens = self.GetExtrapolatedMaterialData(material,  temp,  "dens")
            self.txt_TW_dens.setText(dens)
            elast = self.GetExtrapolatedMaterialData(material,  temp,  "elast")
            elast = str(float(elast) / 1000000000)
            self.txt_TW_Et.setText(elast)
            stress = self.GetExtrapolatedMaterialData(material,  temp,  "stress")
            stress = str(float(stress) / 1000000)
            self.txt_TW_stress_limit.setText(stress)
    else:
        self.ClearTWSelectInputs()

def CheckTWTemp(self, matvar, tempvar):
    for material in self.materials:
        if material.attrib["name"] == matvar:
            temps = material.getiterator("temp")
            for temp in temps:
                if int(temp.text) == int(tempvar):
                    return True
            return False

def GetMaterialData(self, matvar, tempvar, tag):
    for material in self.materials:
        if material.attrib["name"] == matvar:
            temps = material.getiterator("temp")
            for temp in temps:
                if temp.text == tempvar:
                    value = temp.find(tag)
                    return value.text

def GetExtrapolatedMaterialData(self, matvar, tempvar, tag):
    try:
        templist = QStringList()
        for material in self.materials:
            if material.attrib["name"] == matvar:
                temps = material.getiterator("temp")
                for temp in temps:
                    templist.append(temp.text)
        templist.sort()
        target = int(tempvar)
        x1 = max(int(t) for t in templist if t != '' and int(t) < target)
        x2 = min(int(t) for t in templist if t != '' and int(t) > target)
        y1 = float(self.GetMaterialData(matvar, str(x1), tag))
        y2 = float(self.GetMaterialData(matvar, str(x2), tag))
        x = target
        y = y1 - ((y1 - y2) * (x - x1) / (x2 - x1))
        return str(y)
    except Exception, inst:
        return "0"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:23:20+00:00Added an answer on May 20, 2026 at 4:23 pm

    Edit: Ah, I used templist instead of list — hence the confusion. I didn’t mean it to be a one-line function; you still have to do the conversions. (Of course, as Mike DeSimone rightly points out, using list as a variable name is a terrible idea!! So I had a good reason for being confusing. 🙂

    To be more explicit about it, here’s a slightly streamlined version of the function (fixed to test properly for an empty list):

    def GetNextHighTemp(self, temp, templist):
        templist = (int(t) for t in templist if t != '')
        templist = [t for t in templist if t < int(temp)]
        if templist: return max(templist)
        else: return None                   # or raise an error
    

    Thanks to Mike for the suggestion to return None in case of an empty list — I like that.

    You could shorten this even more like so:

    def GetNextHighTemp(self, temp, templist):
        try: return str(max(int(t) for t in templist if t != '' and int(t) < int(temp)))
        except ValueError: return None      # or raise a different error
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: I have a selector like this, which I give to another method as
I have the following code (which largely follows the first example here: http://www.boost.org/doc/libs/1_42_0/libs/multi_index/doc/examples.html )).
Should I feel wary about creating clojure keywords which have non-existent namespaces? An example
For example I have this code: <style> .wrapper { width:1200px; height:800px; } .column {
Can I have an identity (unique, non-repeating) column span multiple tables? For example, let's
I have a non-fixed dimensional matrix M, from which I want to access a
For example I have a pixel with these values: CGFloat red = 34 //
I'm using javascript, and I have an array containing multiple values, which may be
Let's say I for example have this class that generates Fibonacci numbers: public class
Example: We have a conditional field. This conditional field is a radio button with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.