이 가장 우아한 방법으로되지 않을 수도 있습니다,하지만 그것은 내가 생각할 수있는 최초의 일이다 :
def by_y(match):
return match.y
stars = findAll(imageOfStars)
sorted_stars_y = sorted(stars, key=by_y)
finalStars = []
count = 0
for x in range(5): #if you know your grid is 5x5
finalStars.append(sorted(sorted_stars_y[count:count + 5])) #see explanation, if needed
count += 5
for x in finalStars:
click(x)
설명 : 귀하의 예제에서 처음 다섯 개의 별 일치하는 y 값을 가져야합니다. 즉, 모두 맨 위 행이어야합니다. 이제 x 값을 정렬하고 목록에 추가 한 다음 다음 5 단계로 이동하는 등의 작업을 수행 할 수 있습니다.
그리드의 크기가 손 전에 알 수없는 경우, 당신은 달성 할 수있는이 몇 가지 ways-- 그리드는 항상 완벽하게 사각형 인 경우에는 별 전화 번호의 제곱근 찾을 수 :
을
import math #or import sqrt from math, if the square root is the only math function you need.
def by_y(match):
return match.y
stars = findAll(imageOfStars)
sorted_stars_y = sorted(stars, key=by_y)
finalStars = []
count = 0
rows = math.sqrt(len(stars))
for x in range(rows):
finalStars.append(sorted(sorted_stars_y[count:count + rows]))
count += rows
격자가 완벽하지 않은 경우 수행 할 수있는 다른 작업이 있지만 찾으려하지 않는 한이 답변은 약간 길어 졌으므로 나중에 토론을 저장합니다 :)
편집 : 열의 수는 항상 5이므로, 이 같은 행의 :
rows = (len(stars)/5)
rowCount = 0
count = 0
그런 다음 당신이 당신의 별을 반복하는 while 루프를 사용할 수 있습니다 모두가 말하고 완료되면
while rowCount < rows:
finalStars.append(sorted(sorted_Stars_y[count:count+ 5]))
count += 5
rowCount += 1
,이 작업이 당신을 위해 일하지만, 얻을 것이다 @Tenzin의 답이 더 우아합니다 :)
감사, 대답합니다. 그러나 나는 행의 수를 모른다. 나는 열의 수인 5 개만 알고있다. – WhiteFlameAB