python - max, min value of list of lists at 1st column -


hey have short question. lets have list of lists, example:

([[[362, 239]], [[362, 367]], [[386, 367]], [[386, 239]]])

how can max value column 1 , 2 seperatly

answer example should be:

col1_max = 386, col2_max = 367

i tried:

col1_max = max(cnts.iteritems(), key=operator.itemgetter(1))[0] print col1_max

but here errorcode: "attributeerror: 'list' object has no attribute 'iteritems'"

thanks :d

update:

1st deepspace hlp far

i phrase question again in different way right want write code to

  • detect objects

  • centers of these

the code far is:

import imutils import cv2 import os, os.path

dir = 'pics/' numberofpictures = len([name name in os.listdir(dir) if os.path.isfile(os.path.join(dir, name))])

for in range(0, numberofpictures): image = cv2.imread('pics/' + str(i) + '.png', 0) thresh = cv2.threshold(image, 60, 255, cv2.thresh_binary)[1]

cnts = cv2.findcontours(thresh.copy(), cv2.retr_external, cv2.chain_approx_simple) cnts = cnts[0] if imutils.is_cv2() else cnts[1] j = 1 c in cnts: m = cv2.moments(c) if m["m00"] != 0: cx = int(m["m10"] / m["m00"]) cy = int(m["m01"] / m["m00"]) else: continue print cnts j = j + 1 cv2.drawcontours(image, [c], -1, (0, 255, 0), 2) cv2.circle(image, (cx, cy), 7, (255, 255, 255), -1) cv2.puttext(image, "center", (cx - 20, cy - 20), cv2.font_hershey_simplex, 0.5, (255, 255, 255), 2) cv2.imshow("image", image) cv2.waitkey(0) cv2.destroyallwindows()

this alone works want max , min values out of cnts

i use cv2.findcontours creating cnts cnts = [array([[[362, 239]], [[362, 367]], [[386, 367]], [[386, 239]]])]

how can extract max value 1st column , max value 2nd column

thanks hlp :d

in case input list of lists (unlike have in example), should work:

li = [[362, 239], [362, 367], [386, 367], [386, 239]]  def get_max_by_col(li, col):     # col - 1 used 'hide' fact lists' indexes zero-based caller     return max(li, key=lambda x: x[col - 1])[col - 1]  print(get_max_by_col(li, 1)) >> 386  print(get_max_by_col(li, 2)) >> 367 

update if have list of lists of lists, change line
return max(li, key=lambda x: x[col - 1])[col - 1]
in above code to
return max(li, key=lambda x: x[0][col - 1])[0][col - 1].


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -