440,036 Members | 1,963 Online
Need help? Post your question and get tips & solutions from a community of 440,036 IT Pros & Developers. It's quick & easy.

how to get the column index of m-n dimensional array/list?

 P: 20 hi, Need to find the minimum element value in each row of list a[].if the element of row is equal to the min value, i want to get its column index and put in another list b[]. example: a[0]=[1,2,3,6,1] a[1]=[2,3,4,2,5] a[2]=[3,4,7,3,3] The minimum in row a[0] is 1. Element of a[0][0] and a[0][4] is equal to 1. So b=[0,4] But how do i get the index of the column? i tried and got error " 'int' object has no attribute 'index'". This is what i've done. anyone please help.. Expand|Select|Wrap|Line Numbers a=[[0 for i in range(5)]for j in range(3)] b=[0 for i in range(10)]   a[0]=[1,2,3,6,1] a[1]=[2,3,4,2,5] a[2]=[3,4,7,3,3]   for i in range(3):     x=min(a[i])     for j in range(5):         if(a[i][j]==x):             y=a[i][j].index(x)             b[j]=y Oct 14 '10 #1

Using your original code, j is the index (lists explained). Note that this will print multiple finds if the min value exists more that once in the list. Please do not use "i", "l", or "O" as single digit names as they can look like numbers.
Expand|Select|Wrap|Line Numbers
1. a=[]
2. a.append([1,2,3,6,1])
3. a.append([2,3,4,2,5])
4. a.append([3,4,7,3,3])
5.
6. b = []
7. for ctr in range(len(a)):
8.     this_list = a[ctr]
9.     x=min(this_list)
10.     print "\nnew minimum =", x, this_list
11.     for j in range(len(this_list)):
12.         if(this_list[j]==x):
13.             print "found at %d,  x=%d --> %d" % (j, x, this_list[j])
14.             b.append([ctr, j])
15. print b