简体   繁体   English

删除满足条件的列表中的列表

[英]Remove list in lists that satisfied the condition

I'm trying to make a quick OCR for specific use, I know should've just write a preprocessor for normal OCR and that would been faster but this idea came up to me first and I figure I should just try it anyway haha.我正在尝试为特定用途制作一个快速的 OCR,我知道应该为普通的 OCR 编写一个预处理器,这样会更快,但这个想法首先出现在我的脑海中,我想我还是应该尝试一下哈哈。 This program would take a picture on a region of screen and identify the number within it, as of right now, it's only 0 and 1 but I've been working on it and stuck with some problems.这个程序会在屏幕的某个区域拍照并识别其中的数字,截至目前,它只有 0 和 1,但我一直在研究它并遇到了一些问题。 Here is my code这是我的代码

while True:
    if keyboard.is_pressed('`'):
        Firstlist =  list(pyautogui.locateAllOnScreen(image[0], confidence = 0.95,region=( 1570 , 990 , 230 , 70 )))
        print(len(Firstlist))
        Firstlist1 = list(pyautogui.locateAllOnScreen(image1, confidence = 0.95,region=( 1570 , 990 , 230 , 70 ))) + Firstlist
        print(len(Firstlist1))
        print(Firstlist) 
        if len(Firstlist) > 0:
            print(Firstlist[0][0])
            #compare all first instance of that number and eliminate all duplicated with in a different of 5 x pixel
        break

Which would identify some predetermined set of number like this on screen and right now, it would give me a set of coordinate for number zero on screen, here is the result , please ignore other parts, it's just me playing around.它会在屏幕上识别出一些像这样的预定数字,现在,它会给我一组屏幕上数字零的坐标,这是结果,请忽略其他部分,这只是我在玩。 Problem with this is pyautogui.locateAllOnScreen would sometimes generate duplicate value of the same picture within the coordinate ranging from approx 0-5 pixels if not set the confidence level right.问题在于pyautogui.locateAllOnScreen如果未正确设置置信度,有时会在大约 0-5 像素的坐标范围内生成同一图片的重复值。

Example:例子:

Value supposed to be [ (1655,1024,20,26),(1675,1024,20,26) ] but will yield a third value like [ (1655,1024,20,26), (1658,1024,20,26), (1675,1024,20,26) ] .值应该是[ (1655,1024,20,26),(1675,1024,20,26) ]但会产生第三个值,例如[ (1655,1024,20,26), (1658,1024,20,26), (1675,1024,20,26) ]

And that's why I'm trying to make a correction for this.这就是为什么我试图对此进行更正。 Is there anyway to identified if that x value of second duplicate coordinate is within a range of 0-5 pixels to the first coordinate and just delete it, moving the rest up the ladder so that the number would come up right and in order?无论如何要确定第二个重复坐标的 x 值是否在第一个坐标的 0-5 像素范围内,然后将其删除,将其余部分移到梯子上,以便数字按顺序正确显示? Thank you!谢谢!

Note: I'm still working on learning the list removal process by myself, and read the removing list with lambda to me is like gibberish.注意:我仍在努力学习自己的列表删除过程,并且阅读带有 lambda 的删除列表对我来说就像是胡言乱语。 Please forgive me if something is wrong.如果有什么问题,请原谅我。 Have a good day y'all!祝你们有美好的一天!

You can try this.你可以试试这个。

if len(Firstlist) > 2:
    elems = [f[0] for f in Firstlist]  # create a list of just first index
    i = 0
    while i < len(elems) - 1:  # iterate through the list with i
        j = i + 1
        while j < len(elems):  # iterate through the rest of the list with j
            if abs(elems[i] - elems[j]) <= 5:  # if item at index i is within 5 pixels of item at index j
                del elems[j]   # delete item j and continue
            else:
                j += 1    # otherwise move to next item
        i += 1  #  Move to next i item
list1 = [ (1655,1024,20,26), (1658,1024,20,26), (1675,1024,20,26) ]
x = [list1[0]] + [x for x in list1 if abs(list1[0][0] - x[0]) > 5]
print(x)

Output:输出:

[(1655, 1024, 20, 26), (1675, 1024, 20, 26)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM