Skip to content
Advertisement

Python some for loops one action

I have three for loops with one action. How to merge this loops in one?

import lalala
def cv2fn(top, left, width, height, result, path=''):
    coords = {'top': top, 'left': left, 'width': width, 'height': height}
     png = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
     shot = np.array(sct.grab(coords))
     capture = cv2.cvtColor(shot, cv2.COLOR_BGR2GRAY)
     res = cv2.matchTemplate(capture, png, cv2.TM_CCOEFF_NORMED)
     loc = np.where(res >= result)
     return loc

for pt in zip(*cv2fn(915, 1646, 80, 80, 0.65, 'close.png')[::-1]):
     print('close')
 for pt in zip(*cv2fn(919, 1661, 36, 36, 0.60, 'today.png')[::-1]):
     print('close')
 for pt in zip(*cv2fn(716, 1546, 280, 100, 0.20, 'exit.png')[::-1]):
     print('close')

tried:

for (
    pt in zip(*cv2fn(915, 1646, 80, 80, 0.65, 'close.png')[::-1])
    or
    pt in zip(*cv2fn(919, 1661, 36, 36, 0.60, 'today.png')[::-1])
    or
    pt in zip(*cv2fn(716, 1546, 280, 100, 0.20, 'exit.png')[::-1])
    ):
        print('close')

but this trick working only with ‘if’ loops

Advertisement

Answer

I’d suggest itertools.chain

from itertools import chain

for pt in chain(
        zip(*cv2fn(915, 1646, 80, 80, 0.65, 'close.png')[::-1]),
        zip(*cv2fn(919, 1661, 36, 36, 0.60, 'today.png')[::-1]),
        zip(*cv2fn(716, 1546, 280, 100, 0.20, 'exit.png')[::-1])
):
    print('close')
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement