python实现find in功能

本文介绍了一种简单的字符串匹配算法实现,通过双重循环逐个字母比较的方式判断一个字符串是否为另一个字符串的子串。提供了两种实现方式,并通过示例展示了如何使用这些函数。

双重循环逐个字母比较


def strstr(s1,s2):

    tag = False
    len1 = len(s1)
    len2 = len(s2)
    for i in range(0,len2):
        if s2[i] == s1[0]: #匹配到第一个
            for j in range(1,len1):#循环匹配后面几个
                if s2[i+j] != s1[j]:
                    break
                if j == len1-1:#直到最后一个都是一样的则标记成功
                    tag = True

    return tag


简写之后

def strstr(s1,s2):
    tag = False
    len1 = len(s1)
    len2 = len(s2)
    for i in range(0,len2):
        for j in range(0,len1):
            if s2[i+j] != s1[j]:
                break
            if j+1 == len1:
                tag = True
    return tag


print strstr("sel","hello")
print strstr("te","hello")

Python实现VisionPro的FindLine功能通常涉及到图像处理库如OpenCV,这是一个广泛用于计算机视觉任务的强大工具。"FindLine" 功能可能是为了检测图像中的线条,这可以通过以下几个步骤来实现: 1. **导入必要的库**: 首先,需要导入`cv2`(OpenCV的Python接口)以及可能的`numpy`库,用于数值计算。 ```python import cv2 import numpy as np ``` 2. **读取和预处理图像**: 使用`cv2.imread()`加载图像,然后可能对图像进行灰度化、二值化等操作,以便于线条检测。 ```python image = cv2.imread('your_image.jpg', cv2.IMREAD_GRAYSCALE) _, thresholded_img = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) ``` 3. **边缘检测**: 通过`cv2.Canny()`函数找到图像中的边缘,这是寻找线条的一个重要步骤。 ```python edges = cv2.Canny(thresholded_img, low_threshold=50, high_threshold=150) ``` 4. **轮廓检测**: `cv2.findContours()`函数可以找出边缘形成的轮廓,其中包括可能的线条。 ```python contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) lines = [cnt for cnt in contours if cv2.arcLength(cnt, True) > min_length] # 设置合适的最小长度阈值 ``` 5. **绘制线条**: 最后,使用`cv2.drawContours()`将检测到的线条画出来,显示在原始图像上。 ```python for line in lines: x, y, w, h = cv2.boundingRect(line) cv2.line(image, (x, y), (x+w, y+h), color=(0, 0, 255), thickness=2) cv2.imshow("Lines", image) cv2.waitKey(0) # 等待用户按键 cv2.destroyAllWindows() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值