Python 只遍历数组某一部分的正确方法?

Python 只遍历数组某一部分的正确方法?,python,indexing,Python,Indexing,不起作用,我不知道问题出在哪里,因为它不需要切掉无用的东西,只是速度很慢 pxlss = pxls[60:400] 导入pyautogui 导入时间 从PIL导入图像 导入mss 导入mss.tools 进口cv2 将numpy作为np导入 从PIL导入ImageGrab 导入colorsys 时间。睡眠(2) def shootfunc(xc、yc): pyautogui。单击(xc,yc) 游戏区域=[71378328530] def findpixels(PXL): pxlss=pxl

不起作用,我不知道问题出在哪里,因为它不需要切掉无用的东西,只是速度很慢

pxlss = pxls[60:400] 
导入pyautogui
导入时间
从PIL导入图像
导入mss
导入mss.tools
进口cv2
将numpy作为np导入
从PIL导入ImageGrab
导入colorsys
时间。睡眠(2)
def shootfunc(xc、yc):
pyautogui。单击(xc,yc)
游戏区域=[71378328530]
def findpixels(PXL):
pxlss=pxls[60:400]
对于行,枚举中的pxl(pxlss):
对于列,枚举中的pxl(pxl):

如果col>=536且col读取错误消息并进行回溯,则应给出第一个提示:

import pyautogui
import time
from PIL import Image
import mss
import mss.tools
import cv2
import numpy as np
from PIL import ImageGrab
import colorsys

time.sleep(2)

def shootfunc(xc, yc):
    pyautogui.click(xc, yc)

gameregion = [71, 378, 328, 530]

def findpixels(pxls):
    pxlss = pxls[60:400]
    for row, pxl in enumerate(pxlss):
        for col, pxll in enumerate(pxl):
            if col >= 536 and col <= 808 and row <= 515 and row >= 371 and pxll == (102, 102, 102):
                foundpxl = pxll
                print(str(col) + " , " + str(row))
                return [col, row]
                break


def clicktheshit():
    with mss.mss() as sct:
        region = {'top': 0, 'left': 0, 'width': 1920, 'height': 1080}
        imgg = sct.grab(region)
        pxls = imgg.pixels
        chords = findpixels(pxls)
        pyautogui.click(chords[0], chords[1])

xx = 0
while xx <= 3000:
        clicktheshit()
        xx = xx + 1
        time.sleep(.01)
        clicktheshit()
这意味着在这一行中,
和弦
对象,当然不能被索引,而不是您期望的
[col,row]
列表

现在,为什么要得到这个
None
而不是预期的列表很简单:您的
findpixels
函数只有在实际找到匹配项时才返回此列表-否则,该函数没有显式的
return
语句,因此隐式返回
None

瞧,你的问题与“只遍历数组某一部分的正确方法”无关。。。这与不知道如何调试程序有很大关系

import pyautogui
import time
from PIL import Image
import mss
import mss.tools
import cv2
import numpy as np
from PIL import ImageGrab
import colorsys

time.sleep(2)

def shootfunc(xc, yc):
    pyautogui.click(xc, yc)

gameregion = [71, 378, 328, 530]

def findpixels(pxls):
    pxlss = pxls[60:400]
    for row, pxl in enumerate(pxlss):
        for col, pxll in enumerate(pxl):
            if col >= 536 and col <= 808 and row <= 515 and row >= 371 and pxll == (102, 102, 102):
                foundpxl = pxll
                print(str(col) + " , " + str(row))
                return [col, row]
                break


def clicktheshit():
    with mss.mss() as sct:
        region = {'top': 0, 'left': 0, 'width': 1920, 'height': 1080}
        imgg = sct.grab(region)
        pxls = imgg.pixels
        chords = findpixels(pxls)
        pyautogui.click(chords[0], chords[1])

xx = 0
while xx <= 3000:
        clicktheshit()
        xx = xx + 1
        time.sleep(.01)
        clicktheshit()
File "C:/Users/yaahy/PycharmProjects/Testing/testing1.py", line 41, in clicktheshit
pyautogui.click(chords[0], chords[1])
TypeError: 'NoneType' object is not subscriptable