Drake Walter
Drake Walter

Reputation: 137

Basic Python Lists

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

Upvotes: 0

Views: 189

Answers (3)

lethal
lethal

Reputation: 1

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

Upvotes: 0

Blender
Blender

Reputation: 298106

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'

Upvotes: 2

Related Questions