1

Consider the following code:

def temporary4(ratio,x,y):
  results = [] 
  for x in range(1,x):
    results.append(abs((1200*math.log((2**(x/y)),2)-(1200*math.log(ratio,2))))) 
  return results
def intApprox(ratio,x):
  return min(temporary4(ratio,x,x))

Now I use that to make a function that makes a list with a list of 2 elements as elements.

def temporaryTempFinder(ratio):
  list1 = []
  for x in range(12,61,1):
    list1.append([intApprox(ratio,x),x])
  return list1

Test:

temporaryTempFinder(3/2)

This returns a list with terms that look like:

[[1.955000865387433, 12],
 [36.50653759615102, 13],
 [16.240715151101654, 14],
 [18.044999134612567, 15],
 [26.95500086538732, 16],
 [3.927352075789031, 17],
 [31.37833246794594, 18],
 [7.218158760124197, 19],
 [18.044999134612567, 20],
 [16.240715151101654, 21],
 [7.135908225521689, 22],
 [23.69413130017017, 23],
 [1.955000865387433, 24],
 [18.044999134612567, 25],
 [9.647308557695169, 26],
 [9.156110245723653, 27],
 [16.240715151101654, 28],
 [1.4932749966815209, 29],
 [18.044999134612567, 30],
 [5.180807317000472, 31],
 [10.54499913461268, 32],
 [11.045909956296327, 33],
 [3.927352075789031, 34],
 [16.240715151101654, 35],
 [1.955000865387433, 36],
 [11.558512648126111, 37],
 [7.218158760124197, 38],
 [5.737306826920303, 39],
 [11.95500086538732, 40],
 [0.484023524856525, 41],
 [12.330713420327015, 42],
 [4.280582260736196, 43],
 [7.135908225521689, 44],
 [8.621667532054175, 45],
 [2.3928252215692964, 46],
 [12.593298737727878, 47],
 [1.955000865387433, 48],
 [8.249080767265582, 49],
 [5.95500086538766, 50],
 [3.927352075789031, 51],
 [9.647308557695169, 52],
 [0.06820841255716914, 53],
 [9.156110245723653, 54],
 [3.7731826835693028, 55],
 [5.187856277469905, 56],
 [7.218158760124197, 57],
 [1.4932749966815209, 58],
 [9.90940591427352, 59],
 [1.955000865387433, 60]]

I successfully made a function that picks the element with the smallest first element from the list and returns the second element of the element:

def topTempFinder(ratio):
  list1 = []
  for x in range(12,61,1):
    list1.append([intApprox(ratio,x),x])
  return min(list1)[1]

topTempFinder(3/2)

The Result:

53

This is desired because the element which has 53 as its second element has the smallest first element (0.06820841255716914) (refer to the list above for clarification).

My issue now is that I am trying to pick the top 10 elements with the smallest first element and I want the function to return a list of the SECOND element of the chosen elements.

My attempt:

def temporaryTempFinder2(ratio):
  list1 = []
  for x in range(12,61,1):
    list1.append(intApprox(ratio,x))
  return list1

def top10TempFinder(list1):
    final_list = []
  
    for i in range(0, 10): 
        min1 = 100
          
        for j in range(len(list1)):     
            if list1[j] < min1:
                min1 = list1[j];
                  
        list1.remove(min1);
        final_list.append(min1)
          
    return final_list

list1 = temporaryTempFinder(3/2)
list2 = temporaryTempFinder2(3/2)

Trying the code:

top10TempFinder(list2)
top10TempFinder(list1)

Here is where the issue comes, when I use list2, it works as the list has elements without nested lists, however, with list1 I get the error:

TypeError: '<' not supported between instances of 'list' and 'int'

I would like to know how to deal with this. I apologize if the language was confusing and hard to follow, I have attempted to provide examples to make things clear.

2 Answers 2

1

You can use the sorted() built-in function from python with a key argument or heapq.nsmallest() function from python's standard library.

.sorted() solution

>>> temp = temporaryTempFinder(3/2)
>>> def top10(arr):
...     return [i[1] for i in sorted(arr, key=lambda x: x[0])[:10]]
... 
>>> top10(temp)
[53, 41, 29, 58, 12, 24, 36, 48, 60, 46]

heapq.nsmallest() solution

>>> import heapq
>>> def top10(arr):
...     return [i[1] for i in heapq.nsmallest(10, arr)]
... 
>>> top10(temp)
[53, 41, 29, 58, 12, 24, 36, 48, 60, 46]
Sign up to request clarification or add additional context in comments.

1 Comment

heapq.nsmallest seems to be much faster for large lists
0

I think the best approach to this would be to turn your list into a dict then sort that.

info = {l[1]:l[0] for l in lst}
info = dict(sorted(info.items(), key=lambda item: item[1]))
print(list(info.keys())[:10])

output

[53, 41, 29, 58, 12, 24, 36, 48, 60, 46]

#Note: lst is your list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.