By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,036 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
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
  1. a=[[0 for i in range(5)]for j in range(3)]
  2. b=[0 for i in range(10)]
  3.  
  4. a[0]=[1,2,3,6,1]
  5. a[1]=[2,3,4,2,5]
  6. a[2]=[3,4,7,3,3]
  7.  
  8. for i in range(3):
  9.     x=min(a[i])
  10.     for j in range(5):
  11.         if(a[i][j]==x):
  12.             y=a[i][j].index(x)
  13.             b[j]=y
Oct 14 '10 #1

✓ answered by dwblas

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 

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
The minimum value in a list can be obtained with the built-in function min(). The index number of an item in a list can be obtained with list method index(). So:
Expand|Select|Wrap|Line Numbers
  1. >>> [8,6,3,9,2].index(min([8,6,3,9,2]))
  2. 4
  3. >>> 
Oct 14 '10 #2

Expert 100+
P: 621
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 
Oct 14 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.