美文网首页python3自写小工具
python3实现从字符串str1中匹配字符串str2,并返回匹

python3实现从字符串str1中匹配字符串str2,并返回匹

作者: AmanWang | 来源:发表于2020-09-01 18:56 被阅读0次

小工具说明

  1. 第一个函数countLetter():用来查找给定的单字符letter在列表myStrList中的索引位置,返回值为一个列表
  2. 第二个函数countSubString():
    2.1 查找字符串childStr在字符串fatherStr中出现的次数及开始位置,lettersToLower设置为0时区分大小写,设置为非0时不区分大小写
    2.2 返回值为一个字典:
    {
    'childStr_original': 'abcabc', (用来匹配的原始子字符串)
    'fatherStr_original': 'abcabcabcabcabcabc', (用来匹配的原始父字符串)
    'flag': True, (True-匹配成功,False-匹配失败)
    'count': 5, (匹配到的个数)
    'index_start': [0, 3, 6, 9, 12] (匹配到的下标索引列表)
    }
  3. 说明:python3中str的count内嵌方法获取的结果和该小工具有区别,如'aaaaa'.count('aa')=2,countLetter('aa', 'aaaaa')=4

源码

# 计算一个字符letter在给定的list中的位置
def countLetter(letter, myStrList):
    if len(letter) != 1:
        return False
    if letter not in myStrList:
        return False
    tem = []
    for i in range(0,len(myStrList)):
        if letter == myStrList[i]:
            tem.append(i)
    return tem

# 查找字符串childStr在字符串fatherStr中出现的次数及开始位置,lettersToLower设置为0时区分大小写,设置为非0时不区分大小写
def countSubString(childStr, fatherStr, lettersToLower = '0'):
    resDict = {}
    childStr = str(childStr)
    fatherStr = str(fatherStr)
    resDict['childStr_original'] = childStr
    resDict['fatherStr_original'] = fatherStr

    if str(lettersToLower) != '0':
        childStr = childStr.lower()
        fatherStr = fatherStr.lower()

    # 对比用的子字符串如果为空或者不在目标字符串中,直接返回False
    if childStr not in fatherStr or len(childStr) < 1:
        resDict['flag'] = False
        resDict['count'] = 0
        resDict['index_start'] = []
        return resDict
    
    # 两个字符串相同时快速处理
    if childStr == fatherStr:
        resDict['flag'] = True
        resDict['count'] = 1
        resDict['index_start'] = [0]
        return resDict
    
    fatherStr = list(fatherStr)
    childStrList = list(childStr)
    tem, tem2, tem3 = [], [], []
    for item in childStrList:
        a = countLetter(item, fatherStr)
        tem.append(a)
    for jj in range(0, len(tem)):
        tem2.append([x - jj for x in tem[jj]])
    for m in tem2:
        tem3 = list(set(tem2[0]).intersection(m))
    tem3.sort()
    resDict['flag'] = True
    resDict['count'] = len(tem3)
    resDict['index_start'] = tem3
    return resDict

测试结果

# 引用
if __name__ == '__main__':
    print('结果1:',countSubString('test','testtestTest'))
    print('结果2:',countSubString('Test','TEstteSTteSt',lettersToLower='0'))
    print('结果3:',countSubString('Test','TEstteSTteSt',lettersToLower='1'))
    print('结果4:',countSubString('2','3Re23dt2'))
    print('结果5:',countSubString('test','abcsdr'))
    print('结果6:', countSubString(23, 15432345))
    print('结果7:',countSubString('','test'))
    print('结果8:',countSubString('test',''))
    print('结果9:',countSubString('',''))

# 结果
结果1: {'childStr_original': 'test', 'fatherStr_original': 'testtestTest', 'flag': True, 'count': 2, 'index_start': [0, 4]}
结果2: {'childStr_original': 'Test', 'fatherStr_original': 'TEstteSTteSt', 'flag': False, 'count': 0, 'index_start': []}
结果3: {'childStr_original': 'Test', 'fatherStr_original': 'TEstteSTteSt', 'flag': True, 'count': 3, 'index_start': [0, 4, 8]}
结果4: {'childStr_original': '2', 'fatherStr_original': '3Re23dt2', 'flag': True, 'count': 2, 'index_start': [3, 7]}
结果5: {'childStr_original': 'test', 'fatherStr_original': 'abcsdr', 'flag': False, 'count': 0, 'index_start': []}
结果6: {'childStr_original': '23', 'fatherStr_original': '15432345', 'flag': True, 'count': 1, 'index_start': [4]}
结果7: {'childStr_original': '', 'fatherStr_original': 'test', 'flag': False, 'count': 0, 'index_start': []}
结果8: {'childStr_original': 'test', 'fatherStr_original': '', 'flag': False, 'count': 0, 'index_start': []}
结果9: {'childStr_original': '', 'fatherStr_original': '', 'flag': False, 'count': 0, 'index_start': []}

相关文章

  • KMP(看xx)算法及扩展

    题目: 用来求解字符串匹配问题,比如str1中是否包含str2,如果包含就返回str2在str1中开始的位置,不包...

  • KMP算法

    KMP算法是一种字符串匹配算法,对于指定字符串str1和子串str2,返回字串在str1出现的位置索引,str1中...

  • python3实现从字符串str1中匹配字符串str2,并返回匹

    小工具说明 第一个函数countLetter():用来查找给定的单字符letter在列表myStrList中的索引...

  • strcmp函数

    一,strcmp 作用字符串比较如果 str1 小于 str2 返回 < 0; 如果 str1 大于 str2 返...

  • LeetCode 1092. 最短公共超序列 Printing

    给出两个字符串 str1 和 str2,返回同时以 str1 和 str2 作为子序列的最短字符串。如果答案不止一...

  • Strcmp & strncmp

    strcmp C/C++函数,比较两个字符串 设这两个字符串为str1,str2, 若str1==str2,则返回...

  • strstr()函数

    strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在...

  • 面试的三个关键算法

    一、KMP算法 1.定义 str1中是否包含str2,返回初始下标。 2.思路 (1)暴力匹配基于str1,依次匹...

  • 17.连接,复制,输入输出

    1.字符串连接 “str1”+”str2” “str1””str2” 2.字符串复制 “str1”*3 3.键盘输...

  • Oracle函数之字符函数

    1. 拼接字符串函数concat(str1,str2) 功能:拼接两个字符串参数:str1,str2 字符串(数值...

网友评论

    本文标题:python3实现从字符串str1中匹配字符串str2,并返回匹

    本文链接:https://www.haomeiwen.com/subject/bdtbsktx.html