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
361k102 gold badges655 silver badges725 bronze badges
asked Apr 22, 2013 at 3:39
2
  • Are they all integers? Commented Apr 22, 2013 at 3:41
  • 3
    If you have an array of keys and and array of values then a dictionary is a much better data type Commented Apr 22, 2013 at 3:41

3 Answers 3

6
answered Apr 22, 2013 at 3:42
Sign up to request clarification or add additional context in comments.

Comments

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'
answered Apr 22, 2013 at 3:51

1 Comment

+1, but I have a feel the OP was asking for homework so zip and friends might not be allowed.
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,27939 gold badges55 silver badges70 bronze badges
answered Apr 22, 2013 at 4:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.