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
Drake Walter
1371 gold badge2 silver badges8 bronze badges
-
Are they all integers?Ofiris– Ofiris2013年04月22日 03:41:40 +00:00Commented Apr 22, 2013 at 3:41
-
3If you have an array of keys and and array of values then a dictionary is a much better data typeJason Sperske– Jason Sperske2013年04月22日 03:41:46 +00:00Commented Apr 22, 2013 at 3:41
3 Answers 3
- Find the index
iinnumbers_listcorresponding to the smallest element: - Retrieve
test_list[i]
answered Apr 22, 2013 at 3:42
Matt Ball
361k102 gold badges655 silver badges725 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
Blender
300k55 gold badges463 silver badges513 bronze badges
1 Comment
Burhan Khalid
+1, but I have a feel the OP was asking for homework so
zip and friends might not be allowed.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
Comments
lang-py