0

How to find lowest number in Array

Can anyone help me please? I have to access multiple arrays looking like this: [B8,2,B6,4,C5,7,E5,7,F8,3,G4,1], [A2,3,B5,1,C6,3,A5,4] The length of arrays is different, but it always starts with a string of 1 letter and 1 number (i.e. A1) followed by a number between 1 and 8. The content of Arrays differs from 1 pair to maximal 8 pairs. Maybe there is some kind of dictionary-object better suited for this problem, but I didn't find satisfying solutions on the web so far... I want to get the indexOf() entry before lowest number, in my examples that would be indexOf(G4) - 10 for the 1st array and indexOf(B5) - 2 for the 2nd. Is there an easy way to do that or do I have to iterate over complete array every time?

10th Feb 2017, 1:37 PM
Wolfgang Kubik
Wolfgang Kubik - avatar
2 Answers
+ 3
Iterate only once to create a dict and use build-in function 'min()': def arr2dict(arr): dic={} for i in range (0,len(arr),2): dic[arr[i]]=arr[i+1] return dic arr1=['B8',2,'B6',4,'C5',7,'E5',7,'F8',3,'G4',1] arr2=['A2',3,'B5',1,'C6',3,'A5',4] print(arr1) print(arr2) dic1=arr2dict(arr1) dic2=arr2dict(arr2) print(dic1) print(dic2) m1=min(dic1, key=dic1.get) m2=min(dic2, key=dic2.get) print(m1) print(m2) print(arr1.index(m1)) print(arr2.index(m2))
10th Feb 2017, 3:11 PM
visph
visph - avatar
0
thank you very much
10th Feb 2017, 3:56 PM
Wolfgang Kubik
Wolfgang Kubik - avatar