0

I needed help on how to find which test has the lowest number. This code will help explain.

test_list=[]
numbers_list=[]

while True:
    test=raw_input("Enter test or (exit to end): ")
    if test=="exit":
        break
    else:
        test_numbers=input("Enter number: ")
        test_list.append(test)
        numbers_list.append(test_numbers)

If test_list=['Test1','Test2','Test3'] and numbers_list=[2,1,3]

How would I print that Test2 has the lowest number? Since Test2 = 1

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Drake Walter
  • 137
  • 1
  • 2
  • 8

3 Answers3

6
  1. Find the index i in numbers_list corresponding to the smallest element:
  2. Retrieve test_list[i]
Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

You could use zip to zip them together:

>>> zip(numbers_list, test_list)
[(2, 'Test1'), (1, 'Test2'), (3, 'Test3')]

Then use min to find the smallest pair:

>>> min(zip(numbers_list, test_list))
(1, 'Test2')

Finally, you can split the pair up:

>>> number, test = min(zip(numbers_list, test_list))
>>> number      
1
>>> test
'Test2'
Blender
  • 289,723
  • 53
  • 439
  • 496
0

I believe you would be looking to use a dictionary. It would look something like this..

aDict = {'1':'meh','2':'foo'}

sortedDict = sorted(aDict)

lowestValue = sortedDict[0]

print lowestValue
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
lethal
  • 1