我有一个函数number_sort()需要返回一个包含 4 个列表的 2d 列表。函数需要对 0-12、13-25、26-38 和 39-51 之间给出的数字进行排序,如下所示:
鉴于: [2, 2, 3, 4, 1, 13, 15, 51, 10, 15, 28]
回来: [[1, 2, 2, 3, 4, 10], [13, 15, 15], [28], [51]]
我试着用列表理解来做。
def number_sort(cards: list):
return [[item for item in cards if i * 13 <= item <= (i * 12 + 1)] for i in range(4)]
Run Code Online (Sandbox Code Playgroud)
但是当我给出上面给出的确切示例列表时,它返回 [[1], [13], [], []]
有人可以告诉我我做错了什么,我该如何解决?
我正在尝试创建一个程序,在这里需要很长时间来解释,所以我会告诉你们我需要帮助的部分。
这里我需要检测一个矩形(在我们的示例中它将是一个车牌)。它的识别几乎完美,但我希望它更精确。这是我使用的示例图像。
正如您所看到的,它在查找方面做得相当好,但我也想考虑圆角。
这是源代码
import numpy as np
import cv2
import imutils
def find_edges(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
edged = cv2.Canny(image=gray, threshold1=100, threshold2=200)
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
if len(approx) == 4:
screenCnt = approx
break
return screenCnt
image = cv2.imread('img/plate.jpeg')
cnt = find_edges(image)
cv2.drawContours(image, [cnt], -1, …Run Code Online (Sandbox Code Playgroud) 我想要一个由数字组成的列表,乘数随着每一步的增加而增加。基本的嵌套for循环很简单。
numbers = []
i = 1
for x in range(50):
numbers.append(x*i)
i += 1
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用列表理解来做这件事时,它并不像我想要的那样工作。
numbers = [x * i for x in range(50) for i in range(50)]
Run Code Online (Sandbox Code Playgroud)
我知道我在列表理解上做错了,但我不知道如何解决。