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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peachdot
    New Member
    • Aug 2010
    • 20

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

    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..
    Code:
    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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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:
    Code:
    >>> [8,6,3,9,2].index(min([8,6,3,9,2]))
    4
    >>>

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      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.
      Code:
      a=[]
      a.append([1,2,3,6,1])
      a.append([2,3,4,2,5])
      a.append([3,4,7,3,3])
       
      b = []
      for ctr in range(len(a)):
          this_list = a[ctr]
          x=min(this_list)
          print "\nnew minimum =", x, this_list
          for j in range(len(this_list)):
              if(this_list[j]==x):
                  print "found at %d,  x=%d --> %d" % (j, x, this_list[j])
                  b.append([ctr, j])
      print b

      Comment

      Working...