I have three for loops with one action. How to merge this loops in one?
JavaScript
x
17
17
1
import lalala
2
def cv2fn(top, left, width, height, result, path=''):
3
coords = {'top': top, 'left': left, 'width': width, 'height': height}
4
png = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
5
shot = np.array(sct.grab(coords))
6
capture = cv2.cvtColor(shot, cv2.COLOR_BGR2GRAY)
7
res = cv2.matchTemplate(capture, png, cv2.TM_CCOEFF_NORMED)
8
loc = np.where(res >= result)
9
return loc
10
11
for pt in zip(*cv2fn(915, 1646, 80, 80, 0.65, 'close.png')[::-1]):
12
print('close')
13
for pt in zip(*cv2fn(919, 1661, 36, 36, 0.60, 'today.png')[::-1]):
14
print('close')
15
for pt in zip(*cv2fn(716, 1546, 280, 100, 0.20, 'exit.png')[::-1]):
16
print('close')
17
tried:
JavaScript
1
9
1
for (
2
pt in zip(*cv2fn(915, 1646, 80, 80, 0.65, 'close.png')[::-1])
3
or
4
pt in zip(*cv2fn(919, 1661, 36, 36, 0.60, 'today.png')[::-1])
5
or
6
pt in zip(*cv2fn(716, 1546, 280, 100, 0.20, 'exit.png')[::-1])
7
):
8
print('close')
9
but this trick working only with ‘if’ loops
Advertisement
Answer
I’d suggest itertools.chain
JavaScript
1
9
1
from itertools import chain
2
3
for pt in chain(
4
zip(*cv2fn(915, 1646, 80, 80, 0.65, 'close.png')[::-1]),
5
zip(*cv2fn(919, 1661, 36, 36, 0.60, 'today.png')[::-1]),
6
zip(*cv2fn(716, 1546, 280, 100, 0.20, 'exit.png')[::-1])
7
):
8
print('close')
9